简体   繁体   English

使用JavaScript删除值输入onclick

[英]delete value input onclick with JavaScript

How can I delete any value in input when clicked? 单击时如何删除输入中的任何值? I have two functions fillField(input) function clearField(input) and which I want to use in all inputs. 我有两个函数fillField(input) function clearField(input) ,我想在所有输入中使用它。 My code is: 我的代码是:

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

Thank you very much! 非常感谢你!

I solved this the problem. 我解决了这个问题。 For those in need, the code is: 对于有需要的人,代码是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM