2013-12-17 55 views
0

我有以下腳本,我試圖禁用刪除,向上和向下按鈕,如果只有一行存在。有人能告訴我如何做到這一點,我卡住了?禁用按鈕,如果只有行仍然

任何人都可以告訴我如何使用下面的腳本將增量名稱添加到選擇字段?
謝謝

<script> 
$('.addnew').live('click', function(){ 
var thisRow = $(this).parent().parent(); 
newRow = thisRow.clone(true).insertAfter(thisRow); 
newRow.find('input:not(.add)').val(""); 
newRow.find('.remove').show(); 
newRow.find('input.increment').val(parseInt(thisRow.find('input.increment').val())+1); 
}); 

$('.remove').live('click', function(){ 
$(this).parent().parent().remove(); 

}); 

$('.up,.down').click(function() { 

    var row = $(this).parents('tr:first'); 

    if ($(this).is('.up')) { 

     row.insertBefore(row.prev()); 

    } 

    else { 

     row.insertAfter(row.next()); 

    } 

    }); 
    </script> 
+1

您可能想使用「.on()」而不是「.live()」作爲live已在jQuery 1.7版中棄用。 – Scampbell

回答

0

這是從另一個答案由tvanfosson

使用選擇,將選擇所有行並採取長度。

var rowCount = $('#myTable tr').length;

我不知道你的HTML是什麼樣子,但你現在的樣子選擇上述行:

var rowCount = $(this).parents('tr').length;

因此,只有執行你的代碼,如果禁用了按鈕那長度等於一。請注意,它將計算每個嵌套表的所有trs,因此您可能正在檢查除1以外的其他數字。

+1

感謝您帶領我走向正確的方向;儘管我嘗試了你的解決方案,但沒有奏效。但我設法實現this =「var rowCount = $(this).closest(」tr「)。prevAll(」tr「)。length; //獲取錶行 if(rowCount> 0)//將起始行留在原地「 – Alsjka

相關問題