2012-03-16 64 views
0

我已经找遍了这个,但没有太多的运气。如预期jQuery行更改和ID

$("span[id*=Section]").click(function(){ 
    var row = $(this).parents("tr.MoveableRow:first"); 
    if ($(this).is(".up_button")) { 
     row.insertBefore(row.prev()); 
     section = this.id; 
     $("input[name='" + section + "']").val(row.closest("tr").prevAll("tr").length + 1); 
    } else { 
     row.insertAfter(row.next()); 
     section = this.id; 
     $("input[name='" + section + "']").val(row.closest("tr").prevAll("tr").length + 1); 
    } 
}); 

目前,上/下按钮点击时,在TR上移或下移:

所以给这个代码。 “输入[名称=”部分采用传入的跨度ID值并将新值分配给该TR内的隐藏字段。

这样做虽然给我重复的ID。因此,例如,我将第2行移动到第1行。旧行2的隐藏输入值变为1,但原始1的隐藏输入也为1.

所以我需要做的就是让旧的1,新的2.

+0

你能在[JS小提琴](http://jsfiddle.net/)上重现这个吗? – 2012-03-16 22:20:29

+0

您似乎不会更改其他行的值。我错过了什么吗? – 2012-03-16 22:21:53

回答

0

难道你不只是为你移动的行做同样的事情? (因为你是在向上和向下的情况下做同样的事情,你可以简化你的代码位)

$("span[id*=Section]").click(function(){ 
    var row = $(this).parents("tr.MoveableRow:first"); 
    if ($(this).is(".up_button")) { 
     row.insertBefore(row.prev()); 
    } else { 
     row.insertAfter(row.next()); 
    } 

    $("input[name='" + this.id + "']").val(row.closest("tr").prevAll("tr").length + 1); 
    $("input[name='" + row[0].id + "']").val(row.closest("tr").prevAll("tr").length + 1); 
}); 

您也可能会使用.index(),但我不知道你的HTML是什么样子:

$("span[id*=Section]").click(function(){ 
    var row = $(this).parents("tr.MoveableRow:first"); 
    var offset = 1; // Offset of TR being swapped with (up/down) 

    if ($(this).is(".up_button")) { 
     row.insertBefore(row.prev()); 
    } else { 
     row.insertAfter(row.next()); 
     offset = -1; 
    } 

    var position = $('tr.MoveableRow').index(row) + 1; 
    $("input[name='" + this.id + "']").val(position); 
    $("input[name='" + row[0].id + "']").val(position + offset); 
}); 
+0

为我做了这份工作,谢谢! – MrCornfed 2012-03-19 21:19:34