首先,我知道这个问题之前已经在这里被问过 - 我已经查看了这些答案,但由于某种原因它根本不适合我!
我有各种文本字段,我希望有一个“清除”按钮,以便在单击时清空字段的值。
这是我的 JavaScript:
function clear() {
document.getElementById("customerName").value="";
}
我的 HTML 是...
<table border="1" id="orderForm">
<tr>
<th colspan="2">Customer Details</th>
</tr>
<tr>
<td id="font">Customer Name</td>
<td><input type="text" id="customerName"></td>
</tr>
</table>
<button type="button" id="button1" onClick="clear()">Clear</button>
我不知道为什么它不起作用,而且我多年来一直试图让它起作用。
最佳答案
clear()
通常不是一个很好定义的函数名称。它与 document.clear 冲突。
另请记住,您始终可以只使用 <input type="reset" value="clear"/>
这可能更简单! :)
function clearIt() {
document.getElementById('customerName').value = "";
}
<table border="1" id="orderForm">
<tr>
<th colspan="2">Customer Details</th>
</tr>
<tr>
<td id="font">Customer Name</td>
<td><input type="text" id="customerName"></td>
</tr>
</table>
<button type="button" id="button1" onClick="clearIt()">Clear</button>
关于javascript - 使用 Javascript 和 HTML 创建一个清晰的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43439475/