1

私はこのようなHTMLを持っています:

<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>

選択したテキストボックスがある場合は、そのインデックスを検索します。

今のところ、私はこれを書いた:

$('#MyClass .Wrapper').each(function () {

    Index = 0;

    $(this).find('.SomeCheckboxClass').each(function () {
        if ($(this).attr('checked')) {
            Index = $(this).index();
        }
    });

    SomeFunction(Index);
});

これを行うためのより良い方法はありますか?

ありがとう。

4

3 に答える 3

5

次を使用できます:http .map//jsfiddle.net/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);
});
于 2012-06-06T15:54:11.950 に答える
1
$('#Myclass .Wrapper').each(function() {
    var Index = $('.SomeCheckboxClass:checked', this).index();
    SomeFunction(Index);
});

デモ

使用することもできますmap()

$('#Myclass .Wrapper').map(function() {
    var Index = $('.SomeCheckboxClass:checked', this).index();
    SomeFunction(Index);
});

デモ

于 2012-06-06T15:52:00.897 に答える
0

なぜこれをしないのですか?

$('.checkbox').click(function() {
    if($(this).attr('checked')) {
        SomeFunction($(this).index());
    }
});
于 2012-06-06T15:53:30.077 に答える