如何使用jQueryUI获取listitem

如何使用jQueryUI获取listitem,jquery,list,jquery-ui,Jquery,List,Jquery Ui,我试图使用jQuery获取无序列表中ListItems的值。下面的代码几乎可以正常工作,但它始终返回第一个ListItem的值,即使选中了第二个ListItem $(document).ready(function() { $("li").click(function() { $("input:checked").each(function() { alert($("label").attr('InnerText')); });

我试图使用jQuery获取无序列表中ListItems的值。下面的代码几乎可以正常工作,但它始终返回第一个ListItem的值,即使选中了第二个ListItem

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

  • 首张$30.00
  • 第二个$25.00
目前,
$(“标签”)
处理页面上的所有标签(与
$(“输入:选中”)
相同)

您可能需要尝试以下操作,以将选择器仅限于当前匹配的
li
的子项:

$(document).ready(function() {
    $("li").click(function() {
        $("input:checked", $(this)).each($.proxy(function() {
            alert($("label", $(this)).attr('InnerText'));
        }, this));
    });
});
此外,我没有看到
InnerText
属性。你确定那不应该只是
.text()

  • 使用
    .text()
    而不是
    .attr('innerText')
    -
    innerText
    是javascript对象的属性,使用
    .attr()
    无法工作

  • 使用
    $(this).next('label')
    将相关标签添加到复选框中。在
    .each()
    -
    $(此)
    指的是
    复选框
    ,而不是
    单击的


希望这有帮助

$(document).ready(function() {
    $("li").click(function() {
        if ($(this).find("input:checked")) {
            alert($(this).find("label").text());
        }
    });
});

干杯

要使其工作,只需按如下方式更改代码

$("li").click(function() {
    $("input:checked").siblings('label').each(function() {
        alert($(this).text());
    });
});
siblines()
方法获取
输入的兄弟:选中,在本例中,您可以添加标签


工作示例是

您的警报引用了一个不存在的属性。您应该在警报中看到“未定义”。请尝试
.text()
而不是
attr('innerText')
$("li").click(function() {
    $("input:checked").siblings('label').each(function() {
        alert($(this).text());
    });
});