3

以下のコードで、tr要素のid属性 を取得します。

var IDs = [];
$(".head").each(function(){ IDs.push(this.id); });
alert(IDs);

これらのtr要素にはチェックボックスがあります。

私が欲しいのは、チェックボックスがチェックされている場合、私はこれらのtrIDを持っているということです。チェックボックスIDをチェックする必要がありますtr:)

どうすればそれを達成できますか?

4

3 に答える 3

2

チェックされたチェックボックスの親IDを取得するためにこれが必要です...

    var IDs = [];
    $(".head input:checked").each(function(){ IDs.push($(this).parent().attr("id")); });
    alert(IDs);

これが実際の例です...

http://jsfiddle.net/uMfe3/

于 2012-04-17T11:12:00.950 に答える
1

あなたはそうすることができます...

var Ids = $('.head:has(:checkbox:checked)')
           .map(function() { return this.id })
           .get();

jQueryを内部で活用することで実行速度を上げたい場合はquerySelectorAll()、次を使用できます...

var Ids = $('.head').filter(function() {
              return $(this).has('input[type="checkbox"]') && this.checked;
          });

.head...チェックボックスがオンになっている要素のjQueryコレクションを取得します。

于 2012-04-17T11:11:06.670 に答える
0

何かのようなもの

var IDs = [];
//iterate over your <tr>
$(".head").each(function(){ 
    //if there is atleas a checked checkbox
    if($('input:checkbox:checked', this).length > 0){ 
        //add the id of the <tr>
        IDs.push(this.id); 
    }
});

alert(IDs);

またはあなたがすることができます

$(".head input:checkbox:checked").each(function(){ 
    IDs.push(this.id); 
});
于 2012-04-17T11:08:54.753 に答える