当在jquery中使用每个()函数时,例如3个单选按钮,如何检查第3个按钮,以便在检查时发生了什么? 基本上我如何从each()函数中选择我想要使用的元素?
这是我的编码:
HTML:
<form id="orderDefinition" name="orderDefinition">
<fieldset>
<input type="radio" name="radioGroup" /><label for="">radio 1</label>
<input type="radio" name="radioGroup" /><label for="">radio 2</label>
<input type="radio" name="radioGroup" /><label for="">radio 3</label>
</fieldset>
</form>
jQuery的:
var radioBtnCollection = $("#orderDefinition input:radio");
$(radioBtnCollection).each(function(){
// From here, I don't know how to get the element
});
提前致谢。
答案 0 :(得分:2)
您可以使用this operator:
来引用该元素radioBtnCollection.each(function(){
alert(this.name);
});
或者使用提供给函数的参数:
radioBtnCollection.each(function(index, element){
if (index == 2 && element.checked)
alert("3rd element is checked!");
});
如果要对元素执行任何jQuery方法,则需要使用jQuery包装它。对于第一个示例,$(this)
,对于第二个示例$(element)
。
您可以使用:eq(2)而不是每个按钮获取第三个单选按钮:
if ($("#orderDefinition input:radio:eq(2)")[0].checked)
alert("3rd element is checked!");
答案 1 :(得分:0)
使用this
包装为jQuery对象:
$(radioBtnCollection).each(function(){
// this points to the element being iterated
$(this)
});