admin管理员组文章数量:1336632
I have some HTML like this:
<div id="Myclass">
<div class="Wrapper">
<input type="checkbox" class="SomeCheckboxClass">
<input type="checkbox" class="SomeCheckboxClass">
<input type="checkbox" class="SomeCheckboxClass">
</div>
<div class="Wrapper">
<input type="checkbox" class="SomeCheckboxClass">
<input type="checkbox" class="SomeCheckboxClass">
<input type="checkbox" class="SomeCheckboxClass">
</div>
</div>
I want to find the index of a the selected textbox, if any.
For now, I wrote this:
$('#MyClass .Wrapper').each(function () {
Index = 0;
$(this).find('.SomeCheckboxClass').each(function () {
if ($(this).attr('checked')) {
Index = $(this).index();
}
});
SomeFunction(Index);
});
Is there a better way to do this?
Thanks.
I have some HTML like this:
<div id="Myclass">
<div class="Wrapper">
<input type="checkbox" class="SomeCheckboxClass">
<input type="checkbox" class="SomeCheckboxClass">
<input type="checkbox" class="SomeCheckboxClass">
</div>
<div class="Wrapper">
<input type="checkbox" class="SomeCheckboxClass">
<input type="checkbox" class="SomeCheckboxClass">
<input type="checkbox" class="SomeCheckboxClass">
</div>
</div>
I want to find the index of a the selected textbox, if any.
For now, I wrote this:
$('#MyClass .Wrapper').each(function () {
Index = 0;
$(this).find('.SomeCheckboxClass').each(function () {
if ($(this).attr('checked')) {
Index = $(this).index();
}
});
SomeFunction(Index);
});
Is there a better way to do this?
Thanks.
Share Improve this question asked Jun 6, 2012 at 15:50 frenchiefrenchie 52.1k117 gold badges320 silver badges527 bronze badges 8- What if more than one checkbox is checked? – James Allardice Commented Jun 6, 2012 at 15:53
-
the
[checked]
attribute isn't the indicator of whether a checkbox is checked, also i think my answer to this question is relevant. – zzzzBov Commented Jun 6, 2012 at 15:55 - @JamesAllardice: only one checkbox is selected; I've got another function that makes sure of that. – frenchie Commented Jun 6, 2012 at 15:57
- 1 @frenchie, why are you using checkboxes if only one can be selected? You should be using radio buttons. – zzzzBov Commented Jun 6, 2012 at 15:59
- @zzzzBov: because I want to be able to unselect them all. With radio buttons, once you select one, you can't revert to the state "none selected". – frenchie Commented Jun 6, 2012 at 17:21
3 Answers
Reset to default 5You can use .map
: http://jsfiddle/p4nD7/1/.
$("#Myclass .Wrapper :checkbox:checked").map(function() {
return $(this).index(); // index in parent
}).each(function() {
"use strict"; // no object for `this` but just the primitive value
SomeFunction(this);
});
$('#Myclass .Wrapper').each(function() {
var Index = $('.SomeCheckboxClass:checked', this).index();
SomeFunction(Index);
});
DEMO
You can also use map()
$('#Myclass .Wrapper').map(function() {
var Index = $('.SomeCheckboxClass:checked', this).index();
SomeFunction(Index);
});
DEMO
Why not just do this:
$('.checkbox').click(function() {
if($(this).attr('checked')) {
SomeFunction($(this).index());
}
});
本文标签: javascriptjquery getting index of selected checkboxStack Overflow
版权声明:本文标题:javascript - jquery getting index of selected checkbox - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742414862a2470519.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论