4 回答

TA貢獻1770條經驗 獲得超3個贊
.setAttribute('value', "")用下面提到的方法替換方法應該有效
function clearouttheinputboxes()
{
var logopener="----entering clearouttheinputboxes----";
console.log(logopener);
//collect the email address and password.
var emailaddress = document.getElementById("inputEmail");
var password = document.getElementById("inputPassword");
// emailaddress.setAttribute('value', "");
// password.setAttribute('value', "");
//replace the above 2 lines with the below code
emailaddress.value = "";
password.value = "";
var logcloser="----leaving clearouttheinputboxes----";
console.log(logcloser);
}

TA貢獻1828條經驗 獲得超13個贊
我認為您可能使問題過于復雜,并為看似非常簡單的任務使用了太多功能。我相信,除非用戶實際上自己輸入了一些數據,否則目標是在表單輸入中clear或(某個預定值)?set為此,也許以下內容可能會引起興趣 - 一個函數處理兩個按鈕clear并且set(不確定第三個按鈕用于)
document.addEventListener('DOMContentLoaded',e=>{
let div=document.getElementById('inputboxforemailandpassword');
let col=div.querySelectorAll('input[name="email"],input[name="password"]');
let clickhandler=function(e){
col.forEach( input=>{
switch( this.textContent.toLowerCase() ){
case 'clear':
if( input.value=='' || input.value==input.dataset.example )input.value='';
break;
case 'set':
if( input.value=='' && input.value!=input.dataset.example )input.value=input.dataset.example;
break;
}
})
};
div.querySelectorAll('button').forEach( bttn=>{
bttn.addEventListener( 'click', clickhandler );
});
});
label{display:block;width:100%;margin:0.25rem auto;}
label input{float:right}
fieldset{border:1px dotted gray;margin:0.5rem auto}
<div id='inputboxforemailandpassword'>
<fieldset>
<label class='sr-only'>
Email address
<input type='email' name='email' class='form-control' placeholder='Email address' data-example='[email protected]' autocomplete='off' autofocus='true' required />
</label>
<label class='sr-only'>
Password
<input type='password' name='password' class='form-control' placeholder='Password' data-example='P45$W0rd' autocomplete='off' required />
</label>
</fieldset>
<fieldset>
<button class='btn btn-lg btn-primary btn-block'>clear</button>
<button class='btn btn-lg btn-primary btn-block'>set</button>
</fieldset>
</div>

TA貢獻2051條經驗 獲得超10個贊
嘗試這個element.value=""
function clearouttheinputboxes()
{
var logopener="----entering clearouttheinputboxes----";
console.log(logopener);
//collect the email address and password.
var emailaddress = document.getElementById("inputEmail");
var password = document.getElementById("inputPassword");
emailaddress.value=""
password.value=""
var logcloser="----leaving clearouttheinputboxes----";
console.log(logcloser);
}
<div id="inputboxforemailandpassword">
<label for="inputEmail" class="sr-only">Email address</label>
<input type="email" id="inputEmail" class="form-control" placeholder="Email address" required>
<label for="inputPassword" class="sr-only">Password</label>
<input type="password" id="inputPassword" class="form-control" placeholder="Password" required>
</div>
<hr>
<div id="apistatusmessage">
<p>loading...</p>
</div>
<button id = "buttonregister" class="btn btn-lg btn-primary btn-block" onclick="registration()" >
register
</button>
<button id = "clearinput" class="btn btn-lg btn-primary btn-block" onclick="clearouttheinputboxes()" >
clearinput
</button>

TA貢獻1828條經驗 獲得超3個贊
您可以直接更改任何輸入字段的值 = ''(空白)
我正在稍微更新您的代碼。我在做什么,是直接改變輸入字段的值。
function clearouttheinputboxes() {
var logopener="----entering clearouttheinputboxes----";
console.log(logopener);
//----directly changing values of the input fields by "id"
document.getElementById("inputEmail").value = '';
document.getElementById("inputPassword").value = '';
var logcloser="----leaving clearouttheinputboxes----";
console.log(logcloser);
}
如果您遇到問題,請發表評論或回復,以便我更好地理解問題。
添加回答
舉報