jQuery UI 获取 UL 中 LI 元素的排序位置

发布于 2024-10-09 08:47:36 字数 336 浏览 0 评论 0原文

我正在使用 jQuery UI 的可排序功能来处理我的 UL 列表。每次用户对列表进行排序时,我希望每个 li 元素将其“position”属性更新为其在列表中的位置。

<ul>
<li position="1">a</li>
<li position="2">b</li>
<li position="3">c</li>
</ul>

因此,当用户将 c 与 a 交换时,位置也会更新。我尝试使用 .each 但似乎 javascript 不遵循 LI 元素显示的顺序,而是遵循元素创建的顺序。

I'm using jQuery UI's sortable for my UL list. Each time the user sorts the list, I want each li element to update it's "position" attribute to it's position in the list.

<ul>
<li position="1">a</li>
<li position="2">b</li>
<li position="3">c</li>
</ul>

So when a user swaps c with a, the position will also update. I tried to use .each but it seems that javascript doesn't follow the order of how the LI elements are displayed but the order of the element's creation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

摘星┃星的人 2024-10-16 08:47:36

正如另一个答案中提到的,使用 update 就是您所需要的:

$(function() {
    var $sortable = $('ul').sortable({
        update: function(event, ui) {
            var counter = 1;
            $('li', $sortable).each(function() {
                $(this).attr('position', counter);
                counter++;
            });
        }
    });
});

示例链接

As mentioned in another answer, using update is all you need:

$(function() {
    var $sortable = $('ul').sortable({
        update: function(event, ui) {
            var counter = 1;
            $('li', $sortable).each(function() {
                $(this).attr('position', counter);
                counter++;
            });
        }
    });
});

Example link

薄荷梦 2024-10-16 08:47:36

您是否尝试过 :eq 选择器或 index 方法?如果您知道要查找哪个 li 元素的位置,则可以像这样找到该位置:

<ul>
<li id="c">c</li>
<li id="b">b</li>
<li id="a">a</li>
</ul>

var 位置 = $('li#b').index();

Have you tried :eq selector or index method? Provided you know which li element you're trying to find the position of you could find the position like so:

<ul>
<li id="c">c</li>
<li id="b">b</li>
<li id="a">a</li>
</ul>

var position = $('li#b').index();

最初的梦 2024-10-16 08:47:36

您将需要利用可排序的“更新”事件:

$( "ul" ).sortable({
   update: function(event, ui) { 
     var order = $(this).sortable('serialize');
     console.info(order);
   }
});

然后您可以使用“序列化”方法来提取更新的项目顺序。要实现此功能,一个要求是每个列表项的 ID 都包含下划线,因此您需要将 HTML 更新为:

<ul>
   <li id="position_1">a</li>
   <li id="position_2">b</li>
   <li id="position_3">c</li>
</ul>

You'll want to take advantage of the Sortable "update" event:

$( "ul" ).sortable({
   update: function(event, ui) { 
     var order = $(this).sortable('serialize');
     console.info(order);
   }
});

You can then use the "serialize" method to pull the updated order of items. One requirement for this to work is that the IDs of each list item contain an underscore, so you'd want to update your HTML to:

<ul>
   <li id="position_1">a</li>
   <li id="position_2">b</li>
   <li id="position_3">c</li>
</ul>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文