简体   繁体   English

jQuery Sortable - 如何获取当前拖动的元素属性

[英]jQuery Sortable - How to get current dragged element attribute

I am using jqueryui sortable widget. 我正在使用jqueryui可排序小部件。 I need to get the current dragged element's data attribues. 我需要获取当前拖动元素的数据属性。 $(this).data('attribute_name') is not working here. $(this).data('attribute_name')在这里不起作用。 I have also tried some other methods, but not getting the correct result. 我也尝试过其他一些方法,但没有得到正确的结果。

HTML HTML

<ul class="draggable-item" style="min-height:10px;">
   <li data-parent="31" data-id="81" class="ui-state-default">Label</li>
   <li data-parent="31" data-id="86" class="ui-state-default">Max Value</li>
   <li data-parent="31" data-id="83" class="ui-state-default">Unit</li>
   <li data-parent="31" data-id="84" class="ui-state-default">Warning Level High</li>
   <li data-parent="31" data-id="85" class="ui-state-default">Warning Level Low</li>
</ul>

JS JS

$(document).ready(function() {
  $(".draggable-item").sortable({
    start: function( event, ui ) { 
      //Here i need to get current dragged element's 'parent' attribue.
      //console.log(ui.item[0].attributes); - Here i got the entire attribute values in an array. But the order of the array is different in browsers.
    },
  }).disableSelection();
});

Try $(ui.item).data('attribute_name'); 试试$(ui.item).data('attribute_name'); or $(event.currentTarget).data('attribute_name'); $(event.currentTarget).data('attribute_name');

I'm using at UPDATE event: 我在UPDATE事件中使用:

 update: function (event, ui) {

          var attr_id = ui.item.attr('data-id');
        }

Using $(this) will give you the element on which the sortable is initialized. 使用$(this)将为您提供初始化sortable的元素。 Refer the Sortable Documentation . 请参阅可排序文档 The current sortable item is stored in ui object and you can access it using ui.item . 当前可排序项目存储在ui对象中,您可以使用ui.item访问它。

So you can access any attribute of current sortable item by applying functions or methods on ui.item . 因此,您可以通过在ui.item上应用函数或方法来访问当前可排序项的任何属性。 Prefer using $(ui.item) . 喜欢使用$(ui.item)

Demo : http://jsfiddle.net/lotusgodkk/GCu2D/173/ 演示: http//jsfiddle.net/lotusgodkk/GCu2D/173/

$(document).ready(function () {
    $(".draggable-item").sortable({
        start: function (event, ui) {
            var attr = $(ui.item).attr('data-parent');
        },
    }).disableSelection();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM