gpt4 book ai didi

javascript - 使用 JavaScript 删除值输入 onclick

转载 作者:行者123 更新时间:2023-12-01 17:47:40 26 4
gpt4 key购买 nike

单击时如何删除输入中的任何值?我有两个函数 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>
<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>

非常感谢!

最佳答案

我解决了这个问题。有需要的 friend ,代码是:

<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>
     <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>

关于javascript - 使用 JavaScript 删除值输入 onclick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35258085/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com