0

私は次のhtmlを持っています:

<div id="segmenter">
    <div id="variable" name="Zone">
        <h3>Zona</h3>
        <ul>
            <li><input id="category" type="checkbox" value="Center">Center</li>
            <li><input id="category" type="checkbox" value="East">East</li>
            <li><input id="category" type="checkbox" value="South">South</li>
        </ul>
    </div>
    <div id="variable" name="Type of">
        <h3>Tipo de Restaurante</h3>
        <ul>
            <li><input id="category" type="checkbox" value="Freestander">Freestander</li>
            <li><input id="category" type="checkbox" value="Instore">Instore</li>
            <li><input id="category" type="checkbox" value="Mall">Mall</li>
        </ul>
    </div>
</div>

最初に、IDが「変数」のセグメンターのすべてのdiv子を反復処理し、次に、各子「変数」のIDが「カテゴリー」のすべての入力を反復処理します。

Chrome開発ツールの使用:

と:

$("#segmenter > #variable")

私はすべてのdiv変数を取得します:[<div id=​"variable" name=​"Zona">​…​&lt;/div>​, <div id=​"variable" name=​"Tipo de Restaurante">​…​&lt;/div>​]

ここから、私が試したすべてが機能しません。たとえば、次のようになります。

$("#segmenter > #variable").find("#category");

私は4つの入力「カテゴリ」しか取得しません:[<input id=​"category" type=​"checkbox" value=​"Centro">​, <input id=​"category" type=​"checkbox" value=​"Freestander">​, <input id=​"category" type=​"checkbox" value=​"Instore">​, <input id=​"category" type=​"checkbox" value=​"Mall">​]

理由はわかりません。必要なのは、次のような反復です。

var oVariables = $("#segmenter > #variable");
$.each(oVariables, function(){
    var oCategory = $(this).find("#category").text();//in the first call for div-name="zone", only have the first input-value"center"
    //Iterate Again
        //get checked values, etc
});

どうもありがとう。

4

4 に答える 4

1

IDは一意です。コレクションには、代わりにクラスを使用してください。すべてのIDをクラスに変更してから、次のように繰り返します。

$.each($('div.variable'), function(elem){
    $.each($(elem).find('input.category'), function(childElem){
        //Do something with childElem here
    })
});
于 2013-01-31T06:30:12.780 に答える
1

IDを使用しないでください!!! IDはユニークです!!! 代わりにクラスを使用してみてください

于 2013-01-31T06:30:53.307 に答える
1

ID は常に一意である必要があります。

あなたの場合、IDの代わりにクラスを使用してください。お気に入り:

<div id="variable" name="Zone">
    _^_ here
<div class="variable" name="Zone"> //do this for all ids

そしてあなたのセレクター

$("#segmenter > .variable").find(".category");
于 2013-01-31T06:31:05.167 に答える
0

同じ問題があります。新しい子タグにアクセスする必要があります。

私のやり方:

var list_group_items = $("a.list-group-item");
    $.each(list_group_items,function(index,value){
        $(list_group_items[index]).hover(function(){
            $.each($(list_group_items[index]).find('small'), function(childElem,Element){
                console.log(Element);
                TweenLite.to(Element, 2, {"visibility":"visible"});
            });

        },function(){
            $.each($(list_group_items[index]).find('small'), function(childElem,Element){
                //Do something with childElem here
                TweenLite.to(Element, 2, {"visibility":""});
            });

        });
    });

注 1:$.each関数には 2 つの引数があり、最初の 1 つはiterate のインデックスで、2 番目はelementです。

注2:アニメーションにはTeenMax Libraryを使用しています。

于 2018-08-18T21:30:03.020 に答える