开发者

JQuery - get id of div element with same class name

开发者 https://www.devze.com 2023-03-07 06:54 出处:网络
I need to pick id od selectable when Test1 or Test2 are clicked. <div class=\"selectable\" id=\"1\">

I need to pick id od selectable when Test1 or Test2 are clicked.

    <div class="selectable" id="1">
         <span class="ui-state-default">Test1</span></asp:Label>
     </div>

    <div class="selectable" id="2">
         <span class="ui-state-default">Test2</span></asp:Label>
    </div>

I try somethnig, but i only get first time good value, i get always same value next times.

        $(".selectable").selectable({
            stop: function() {
            $(".ui-selected", this).each(function() {
                alert($(".ui-开发者_运维问答selected").closest("div").attr("id"));
                });
            }
        });


You need to use $(this) in your .each() function instead of $(".ui-selected"):

$(".selectable").selectable({
    stop: function() {
        $(".ui-selected", this).each(function() {
            alert($(this).closest("div").attr("id"));
        });
    }
});

Otherwise only the first .ui-selected (and consequently the first parent div) will be looked at, and you'll only get the first div's ID.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号