我有一个表单,其中有三个文本字段,名称,纬度和经度。这些表格已经预先填好了,但我想点击纬度和经度时要清除所有预先填好的文字值。我怎么能这样做?
答案 0 :(得分:0)
您只需收集对数组中所有输入元素的引用,然后迭代数组,在每次迭代时清除内容:
// Get a reference to the button
var btn = document.getElementById("clear");
// Set the button up for a click event handler
btn.addEventListener("click", function(){
// Get all the input elements that are text fields and convert that list into an array
// Loop through that array and clear the value of each text field
Array.prototype.slice.call(document.querySelectorAll("input[type='text']")).forEach(function(element){
element.value = "";
});
});

<input type="text" value="abc">
<input type="text" value="abc">
<input type="text" value="abc">
<input type="text" value="abc">
<input type="text" value="abc">
<button id="clear">Clear all text fields</button>
&#13;