[英]jQuery: How to get a set of elements?
I have a simle html
div
with a variable number of input checkboxes and i whould like to chek which chekbox is cheked and which isn't 我有一个带有可变数量的输入复选框的simle html
div
,我想检查哪个chekbox被选中,哪个不是
HTML code: HTML代码:
<div>
<input type="checkbox" val="1"/>Check One<br/>
<input type="checkbox" val="2" checked="checked"/>Check Two<br/>
</div>
Javascript code: JavaScript代码:
var $kids = $("div").children("input");
for (var i = 0; i < $kids.length; i ++) {
// if i alert($kids[i]); i get [object HTMLInputElement] how can i work with it?
alert ($kids[i].val()); // doesn't work
}
In jQuery site i read children()
method can "Get a set of elements containing all of the unique immediate children of each of the matched set of elements." 在jQuery网站中,我读到children()
方法可以“获取包含每个匹配元素集的所有唯一直接子元素的元素集”。 but what is a "set of elements"and how can I work with it? 但是什么是“元素集”,我该如何使用呢?
$("input:checkbox:checked")
or $("input:checkbox:not(:checked)")
$("input:checkbox:checked")
或$("input:checkbox:not(:checked)")
You should add an id to your container to limit the options... 您应该在容器中添加一个ID以限制选项...
<div id="container"> inputs here </div>
Then you can use... 然后您可以使用...
var checked = $("#container input:checkbox:checked");
var unchecked = $("#container input:checkbox:not(:checked)");
EDIT : 编辑 :
I should have read your question more carefully you were asking how to iterate over the the set. 我应该更仔细地阅读您的问题,您正在询问如何遍历集合。 as has already been stated you can do: 如前所述,您可以执行以下操作:
$('div input:checked').each(function(){
// do something with the item this is always the current element in the iteration
});
or 要么
$.each(yourVar, function(i, e){
// your var can be anything iterable really... an object, an array i is the index and e is the value for that index
});
$('div input:checked');
should work. 应该管用。
Working with a set of elements is very similar to working with a single element in jQuery.. There are a lot of methods you can use to split the segments up .each()
allows you to work with each matched element, .css()
, .animate()
, .show()
etc all work with all the elements matched in the set as well. 使用一组元素与使用jQuery中的单个元素非常相似。有很多方法可用于将段分割.each()
允许您使用每个匹配的元素.css()
, .animate()
.show()
等也可以与集合中匹配的所有元素一起使用。 You can used .get()
or just an array index [0]
to get single elements out of the jQuery matched set. 您可以使用.get()
或仅使用数组索引[0]
从jQuery匹配集中获取单个元素。
input:checkbox
该选择器将为您提供所有复选框: input:checkbox
input:checkbox:checked
该选择器将为您提供所有选中的复选框: input:checkbox:checked
input:checkbox:not(:checked)
该选择器将为您提供所有未选中的复选框: input:checkbox:not(:checked)
You can access the elements with $kids[i], but what you're getting back isn't a JQuery object, it's a standard DOM element. 您可以使用$ kids [i]访问元素,但是返回的不是JQuery对象,而是标准的DOM元素。 Therefore you can't call the .val() method, because that doesn't exist for DOM objects. 因此,您不能调用.val()方法,因为DOM对象不存在该方法。
So really you only have to make a small modification: 因此,实际上您只需要进行一些小的修改:
var $kids = $("div").children("input");
for (var i = 0; i < $kids.length; i ++) {
// if i alert($kids[i]); i get [object HTMLInputElement] how can i work with it?
alert ($kids[i].checked); // does work :-)
}
Hope this helps! 希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.