循环所有输入框并清除它们 [英] Loop though all input boxes and clear them

查看:124
本文介绍了循环所有输入框并清除它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用javascript循环遍历页面上的所有输入框,并清除其中的数据(使其全部为空)?

How can I, using javascript, loop through all input boxes on a page and clear the data inside of them (making them all blank)?

推荐答案

尝试这段代码:

​$('input').val('');

它循环所有输入元素,它是将其值设置为空字符串。
这是一个演示: http://jsfiddle.net/maqVn/1/

It's looping over all input elements and it's setting their value to the empty string. Here is a demo: http://jsfiddle.net/maqVn/1/

当然如果你没有jQuery:

And of course if you don't have jQuery:

var inputs = document.getElementsByTagName('input');
for (var i = 0; i < inputs.length; i += 1) {
    inputs[i].value = '';
}​​​​

由于我喜欢功能样式,您可以使用: / p>

Since I prefer the functional style, you can use:

Array.prototype.slice.call(
  document.getElementsByTagName('input'))
  .forEach(function (el) {
    el.value = '';
});

这篇关于循环所有输入框并清除它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆