我想用一个按钮清除给定 DIV 中所有文本字段的内容。我正在使用这个 javascript 代码:
<div id="block1">
<table>
<tr>
<td><!--somefields--></td>
</tr>
<tr>
<td><input type="button" onclick="clear('block1')" value="clear"></td>
</tr>
</table>
</div>
<script type="text/javascript">
function clear(fieldid) {
document.getElementById(fieldid).childNodes[3].innerHTML='';
}
</script>
但它不起作用。怎么了?
谢谢。
最佳答案
下面一段代码为您工作,带有示例输入文本框
<div id="block1">
<table>
<tr>
<td><input type="text" value="Hello"/></td>
</tr>
<tr>
<td><input type="button" onclick="clearFields('block1')" value="clear"></td>
</tr>
</table>
</div>
<script type="text/javascript">
function clearFields(fieldid) {
var container, inputs, index;
container = document.getElementById(fieldid);
inputs = container.getElementsByTagName('input');
for (index = 0; index < inputs.length; ++index) {
if(inputs[index].type =="text")
inputs[index].value = '';
}
}
</script>
关于javascript - 仅使用 javascript 清除 DIV 中的所有文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32901512/