2010-10-27 42 views
0

我正在使用以下腳本將<tr>從表中的任意位置移動到該表的頂部。這發生在ajax表單提交後。該form's - 而不是button's - 類是「頂級」如何使用jQuery禁用/啓用多個按鈕

$(".top").click(function() { 
    var thisRow = $(this).parent().parent(); 
    var firstRow = thisRow.parent().find("tr:first").not(thisRow); 
    thisRow.insertBefore(firstRow); 
}); 

一旦物品到達表的頂部,我想禁用Send Item To Top按鈕。因爲在每個<tr>另外兩個按鈕的問題變得複雜:Move Item Up By OneMove Item Down By One(包括AJAX,太)

一旦<tr>移動到頂部,我想與Send Item To Top按鈕沿禁用Move Item Up By One。另外,我想在單擊Move Item Down By One時重新啓用它們。

下面的腳本被用於交換<tr>的上/下移動位置。

注:我不能指定任何我的按鈕的id或類,因爲他們的形式是動態生成的。我只能將表單分配給一個ID或一個類。

// Move item down/up 
$(".up,.down").click(function(){ 
    var row = $(this).parents("tr:first"); 
    if ($(this).is(".up")) { 
     row.insertBefore(row.prev()); 
    } else { 
     row.insertAfter(row.next()); 
    } 
}); 

下面是HTML:

<tr id="item_168" class="element168"> 
    <td><form action=".." class="top..." method="post" onsubmit="..."><input onclick="" type="submit" value="&uarr;" /></form></td> 
    <td class="action"><form action="up..." class="up" method="post" onsubmit="..."><input onclick="" type="submit" value="&uarr;" /></form></td> 
    <td class="action"><form action="down..." class="down" method="post" onsubmit="..." title="Move one"><input onclick="" type="submit" value="&darr;" /></form></td> 
</tr> 
+0

請給你的瀏覽器解釋HTML結構以及 – 2010-10-27 12:40:20

回答

1

我是很新的jQuery的,但你可以做

$(row).find('.up,.top').find('input[type=submit]').attr('disabled', 'disabled') 
+0

如果你拿出'[type = button]',那麼它就可以工作......謝謝,我在兩種情況下完成了這項工作......將項目移動到頂端,然後將其向下移動。當「移動項目」按鈕到達頂端時,我可以嘗試找出如何去做......請編輯您的答案以反映[type = input]部分,以便我可以接受它。 – Mohamad 2010-10-27 13:08:14

+0

如果可能的話,我寧願編輯它,以便類型正確...您可以向我展示其中一個按鈕的HTML嗎? – Chowlett 2010-10-27 13:43:15

+0

克里斯,當然,我猜這個類型只需要提交而不是按鈕...

Mohamad 2010-10-27 13:47:41

0

給你的按鈕類或ID,並使用這些掛鉤啓用/禁用/隱藏按鈕

+0

莫因,它不是一個選項,因爲表單是通過我的框架API動態生成的,我只能將類應用到表單。 – Mohamad 2010-10-27 12:37:55

+0

此外,我甚至不知道代碼會在我的例子中出現在哪裏。 – Mohamad 2010-10-27 12:40:20

1

這裏的事件處理程序分爲兩(爲了便於閱讀):

$('#yourTableID').delegate('.up', 'click', function() { 
    var row = $(this).closest('tr'); 
    var index = row.index(); 

    if(index != 0) { 
    row.insertBefore(row.prev()); 

    if(index == 1) { 
     $(this).attr('disabled', 'disabled'); 
    } 
    } 

}) 
.delegate('.down', 'click', function() { 
    var row = $(this).closest('tr'); 
    var index = row.index(); 

    if(index < row.siblings().length) { 
    row.insertAfter(row.next()); 

    if(index + 1 == row.siblings().length) { 
     $(this).attr('disabled', 'disabled'); 
    } 
    } 

}); 

編輯︰改變row.insertAfter(row.prev ())到row.insertAfter(row.next())。糟糕!

而且,這裏的小提琴,以檢查它在行動:http://jsfiddle.net/pn3MN/

+0

謝謝約翰,但是一旦他們被禁用,按鈕將保持禁用狀態。向下移動不會重新啓用它們。此外,還有移動項目的其他按鈕頂部:/ – Mohamad 2010-10-27 12:50:38

+0

是的,我開始玩弄小提琴,注意到我沒有做完整的圈子。但是誰來爲整個解決方案做好準備? :)希望這有助於這個概念,並且你不希望得到答案:p – 2010-10-27 12:53:59

+0

哈哈!像我這樣的業餘愛好者會這樣做:D ......但上述的一條線路解決方案很有效。大部分。還有一個MOD,它已經完成了。不過感謝您的幫助! – Mohamad 2010-10-27 13:10:28