使用JQuery获取ListItem值

1 jquery listitem

我正在尝试使用JQuery在无序列表中获取ListItems的值(见下文).下面的代码接近工作,但它总是返回第一个ListItem的值,即使检查第二个ListItem也是如此.

谢谢,拉斯.

    $(document).ready(function() {
        $("li").click(function() {

            $("input:checked").each(function() {

                alert($("label").attr('InnerText'));

            });
        });
    });

<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>
Run Code Online (Sandbox Code Playgroud)

Joh*_*ter 8

在你的每次回调中,你并没有限制选择器的范围,它只是抓住你在调用.attr时找到的第一个标签.尝试类似以下内容......

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

这个选择器选择复选框周围的元素而不是整个文档