单击时如何删除输入中的任何值?
我有两个函数fillField(input)
function clearField(input)
,我想在所有输入中使用它们。
我的代码是:
<script>
// identify form elements:
var search_code = document.getElementById('search_code');
var insert_code = document.getElementById('insert_code');
var result = document.getElementById('result');
var button = document.getElementById('button');
// respond to button click
button.onclick = function validate() {
// show verification result:
if(search_code.value == insert_code.value) {
result.textContent = 'code is ok';
result.className = "ok";
} else {
result.textContent = 'code is not ok';
result.className = "not-ok";
}
// clear input when wrong:
if (search_code.value !== insert_code.value) {
insert_code.value = '';
}
return false;
};
function fillField(input) {
if(input.value == "any value")
input.value="";
};
function clearField(input) {
if(input.value == "")
input.value= "any value";
};
insert_code.oninput = function () {
result.textContent = ''; // clear result;
};
</script>
&#13;
<form>
<input type="text" name="search_code" onblur="fillField(this,'insert code');" onfocus="clearField(this,'insert code');" id="search_code" value=""/><br/>
<input type="" name="insert_code" onblur="fillField(this,'scan code');" onfocus="clearField(this,'scan code');" id="insert_code" placeholder="scand code"value=""/><br/><br/>
<input type="submit" id="button" name="button" value="validation" />
</form>
&#13;
非常感谢!
答案 0 :(得分:0)
我解决了这个问题。对于有需要的人,代码是:
<script>
// identify form elements:
var search_code = document.getElementById('search_code');
var insert_code = document.getElementById('insert_code');
var result = document.getElementById('result');
var button = document.getElementById('button');
// respond to button click
button.onclick = function validate() {
// show verification result:
if(search_code.value == insert_code.value) {
result.textContent = 'cod gasit';
result.className = "ok";
} else {
result.textContent = 'codul nu este corect';
result.className = "not-ok";
}
// clear input when wrong:
if (search_code.value !== insert_code.value) {
insert_code.value = '';
}
return false;
};
//clear text when click on input
function clearField(input) {
input.value = "";
};
</script>
&#13;
<form>
<input type="text" name="search_code" onfocus="clearField(this, this.placeholder='');" onblur="this.placeholder='introdu codul'" id="search_code" placeholder="introdu codul" autocomplete="off" value=""/><br/>
<input type="" name="insert_code" onfocus="clearField(this, this.placeholder='');" onblur="this.placeholder='scaneaza codul'" id="insert_code" placeholder="introdu codul" autocomplete="off" value=""/><br/><br/>
<input type="submit" id="button" name="button" value="verifica COD" />
</form>
<div id="result"></div>
&#13;