如何在不使用 jQuery 的情况下使用 JavaScript 运行带有类名的每个选中的复选框
javascript 中是否有与下面的 jquery 代码直接等效的代码?
$('.checkbox').each(function() {
if ($(this).is(':checked')) {
//logic here
}
});
我正在尝试使用 class = 'checkbox'
遍历页面上的所有复选框 - 客户端不想使用 jQuery,所以我需要上述的替代方案。
我希望我可以避免从头开始编写一个长函数来执行此操作,而只需使用 JavaScript 中内置的东西,但看起来这是不可能的。
Is there an immediate equivalent in javascript for the below jquery code?
$('.checkbox').each(function() {
if ($(this).is(':checked')) {
//logic here
}
});
I'm trying to run through all the checkboxes on a page with class = 'checkbox'
- the client doesn't want to use jQuery, so I need an alternative for the above.
I'm hoping I can avoid writing a long function from scratch to do this and simply use something built-in to JavaScript, but it's looking like it's not possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
许多旧版浏览器不支持
querySelectorAll
或getElementsByClassName
,因此您必须循环遍历这些浏览器中的所有元素。不过,最好先检查这些功能。
其次,你永远不应该使用
$(this).is(":checked")
— 甚至在 jQuery 中也不应该使用 — 在寻找时,这是一条非常缓慢的路径>this.checked
。这应该可以帮助您继续:
在上面的示例中,您可以将
base
的值更改为任何元素。这意味着,如果所有这些元素都有一个共同的父节点或祖先节点,您可以将该元素设置为基础,并且它应该运行得更快,例如:Many older browsers don't support
querySelectorAll
orgetElementsByClassName
, so you'd have to loop over all<input>
elements in those browsers. It's always best to check for those functions first, though.Secondly, you should never use
$(this).is(":checked")
— not even in jQuery — it's a very slow path to take when looking forthis.checked
.This should get you going:
In the example above, you can change the value of
base
to any element. This means that, if all these elements have a common parent or ancestor node, you can set that element as the base and it should run faster, e.g:请参阅下面的评论。对于旧版本的 IE 和其他旧浏览器,您可以使用
getElementsByTagName
。See comments below. you can use
getElementsByTagName
for older versions of IE and other older browsers.尝试以下操作
Try the following
JavaScript 具有通过 ID 或标记名称获取 DOM 元素的内置方法,但通过类进行选择 旧版本的 IE 不支持。但是,获取所有
input
并测试它们的checkbox
类型会相当快:您还可以测试它们的类是否为“checkbox”,但这会变得复杂如果他们有多个班级。如果他们不这样做:
JavaScript has built-in methods for getting DOM elements by ID, or by tag name, but selecting by class isn't supported in older versions of IE. However, it would be fairly fast to obtain all
input
s and test them for thecheckbox
type:You can also test if their class is "checkbox", but this gets complicated if they have more than one class. If they don't:
这有点依赖于浏览器。但在现代浏览器中,您可以使用
document.getElementsByClassName('checkbox')
来获取要迭代的数组,然后is(':checked')
成为更常见的if(array[i].checked){}
。请随意阅读有关兼容浏览器的信息。您会发现它在 Internet Explorer 5.5、6 和 7 中不起作用。
我认为 jQuery 在 这里很热闹。
所以它可能看起来像:
最后注意:getElementsByTagName('*') 不适用于 Internet Explorer 5.5。
It's kind of browser dependent then. But with a modern browser you'd use
document.getElementsByClassName('checkbox')
to get an array that you'd iterate through, thenis(':checked')
becomes the more commonif(array[i].checked){}
.Feel free to read about the compatible browsers. You'll find that it doesn't work in Internet Explorer 5.5, 6, and 7.
I think jQuery works around this in sizzle about here.
So it might look like:
Final note: getElementsByTagName('*') doesn't work with Internet Explorer 5.5.