使用 javascript 清除按钮单击上的输入框
我有一个动态创建文本输入框的程序。我想在单击清除按钮时清除框中的内容。
var elems = document.getElementsByClassName(CLId);
var myLength = elems.length;
var total = 0;
for (var i = 0; i < myLength; ++i) {
if(elems[i].value != null && elems[i].value > 0){
var el = elems[i];
}
}
我用它来获取盒子的值,但不知道如何让它们为空......
I've a program which creates text input boxes dynamically. I want to clear the contents of the boxes on the click of clear button.
var elems = document.getElementsByClassName(CLId);
var myLength = elems.length;
var total = 0;
for (var i = 0; i < myLength; ++i) {
if(elems[i].value != null && elems[i].value > 0){
var el = elems[i];
}
}
I use this to get the values of the boxes, but don't know how to make them empty....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 getElementsByTagName (类版本还不能跨浏览器)
并为具有所需类的元素将值设置为“”。
(或者您可以对整个表单使用重置输入框)
use getElementsByTagName (the class version is not cross-browser yet)
and set the value to "", for the elements with the desired class.
(or you can use a reset input box for the entire form)
如果是表单,则只需使用
标记 (HTML)。否则你会想要:
if it is in a form, you can simply use the
<input type="reset" value="clear"/>
tag (HTML). Otherwise you will want:您可以尝试设置该值:
You could try setting the value:
循环中的
elems[i]
值将是一个字符串,因此您可以测试它的长度或其与空字符串 ("") 的等价物。您也可以将其值设置为“”来清除它。The value of
elems[i]
in your loop is going to be a string, so you can test its length or its equivalent to the empty string (""). You can also set its value to "" to clear it.