2012-12-05 29 views
1

我有一個問題需要一些JS代碼才能工作。我們正在做的是通過拖放對信息表進行排序,然後更新顯示移動項目的排序順序號的輸入字段以及所有其他項目。使用jquery進行排序後使用jquery更新輸入值

的我在做什麼的一個例子是在這裏:http://artmarketnetwork.com/sort_example.html

顯然打破,我使用jQuery排序,下面的代碼是我用,可排序的作品,函數的執行更新其他輸入語句未被working.`

$(document).ready(function(){ 
    $("#AttachmentWeight").live("change keyup", function() { 
     $("#UserDashboardWeightForm").submit(); 
    });  

    $(".tablesorter tbody").sortable({ 
     opacity: 0.6, 
     cursor: 'move', 
     update: function() { 
      $('table#summary tbody tr:odd').removeClass('alt-row'); 
      $('table#summary tbody tr:even').addClass('alt-row'); 
      var order = $(this).sortable("serialize"); 
      $.post("/attachments/sortable/", order); 
          updateInputs(); 
     }         
    }); 

}); 

function updateInputs() { 
    $("#AttachmentWeight").each(function() { 
     var aid = $(this).attr('rel'); 
     alert(aid); 
     var weight = $(this).load("/attachments/getWeight/" + aid); 
     alert(weight); 
     $(this).val(weight); 
    }); 
} 

`

上述呼叫加載( 「/附件/ ...」)是獲得重量#的項目。它的一個部分只返回一個數字,所以我們知道它的重量。

有沒有其他人對此有過任何成功?我有一種感覺,它很簡單,我錯過了。

回答

1

我瀏覽過網站上的標記。 你不應該在DOM中有重複的ID。用class替換id。

<input name="data[Attachment][0][weight]" type="text" value="1" rel="3334" style="width:30px;" class="AttachmentWeight"> 


function updateInputs() { 
    var attachments = $(".AttachmentWeight").each(
     var $this = $(this), 
      id = $this.attr("rel"), 
      setWeight = function(value){ 
       $this.val(value); 
      }; 
     $.get("/attachments/getWeight/" + id).done(setWeight); 
    ); 


} 

注:

決不重複的ID!

.load從服務器加載數據並將返回的html注入匹配的DOM元素。簡單的$ .get會在不注入DOM的情況下加載數據。

+0

謝謝你,我們決定走另一條路線,但希望這可以幫助路上的其他人。我不知道重複的ID是一個問題,我將在未來繼續考慮這一點。 –