1

JQuery を使用して、順序付けられていないリストで ListItems の値を取得しようとしています (以下を参照)。以下のコードはほとんど機能していますが、2 番目の ListItem がチェックされていても、常に最初の ListItem の値を返します。

<script>
    $(document).ready(function () {
        $("li").click(function () {
            $("input:checked").each(function () {
                alert($("label").attr('InnerText'));
            });
        });
    });
</script>

<div>
    <ul class="AspNet-CheckBoxList-RepeatDirection-Vertical">
        <li class="AspNet-CheckBoxList-Item">
            <input id="CheckBoxList1_0" type="checkbox" name="CheckBoxList1$0" value="first1" />
            <label for="CheckBoxList1_0">First $30.00</label>
        </li>
        <li class="AspNet-CheckBoxList-Item">
            <input id="CheckBoxList1_1" type="checkbox" name="CheckBoxList1$1" value="second2" />
            <label for="CheckBoxList1_1">Second $25.00</label>
        </li>
    </ul>
</div>
4

1 に答える 1

8

各コールバックでは、セレクターのスコープを制限しておらず、.attr を呼び出すときに最初に見つかったラベルを取得しているだけです。次のようなものを試してください...

$("input:checked").each(function() {
    $(this).next("label").text(); //Or
    $(this).parent().find("label").text(); //Depending on your markup
});

これにより、ドキュメント全体ではなく、チェックボックスの周囲の要素にセレクターが適用されます

于 2009-04-08T18:43:33.703 に答える