簡體   English   中英

jQuery使用表對象迭代表行

[英]jQuery iterate table rows using table object

正在處理拖放表行並更新數據庫中的順序。

我為所有表(如附件)編寫了一個通用函數。 請建議如何從表對象迭代表行。 另外,也請告知任何有用的插件以實現拖放功能。

錯誤消息: 未捕獲錯誤:語法錯誤,無法識別的表達式:[object Object]> tbody> tr這是提琴手鏈接: https : //jsfiddle.net/22yLakr3/5/

$("table").sortable({
            items: "tr",
            cursor: 'move',
            opacity: 0.6,
            update: function () {
                var abc = $(this);
                $(abc + ' > tbody > tr').each(function () {                      
                   var id = $(this).attr('data-k');                 
                });

            }
        });

在此處輸入圖片說明

您需要注意兩點:

第一個是:為什么要在更新事件內部循環? 如果您需要獲取當前更新元素的數據-k,則解決方案有所不同

第二個是:如果需要在每一行上循環,則不能將對象用作選擇器。

在下面(請參閱更新事件功能中的注釋):

$(function () {
  $("table").sortable({
    items: "tr",
    cursor: 'move',
    opacity: 0.6,
    update: function (event, ui) {
      // on update you may take the data-k atribute value simply:
      var lDataK = ui.item.data('k');


      // If instead on update (triggered when the user stopped sorting and 
      // the DOM position has changed)
      // you need to look for all rows you need to write:
      $(this).find('tbody > tr').each(function (index, element) {
        var id = element.getAttribute('data-k');
        // now you can use this value
      });

    }
  });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM