2011-11-30 43 views
1

我用下面的錶行內禁止所有形式的元素:禁用所有的表單元素,但一個

$(".chkbx").change(function(){ 
    var $this = $(this); 
    if ($this.is(":checked")) { 
     $this.closest("tr").find(":input").attr("disabled", false); 
    } else { 
     $this.closest("tr").find(":input").attr("disabled", true); 
    } 
}); 

它確實很大。問題 - 它禁用所有,包括.chkbx。我需要保持此類(chkbx)始終啓用的複選框。我如何從函數中排除它?

+2

'$ this.closest( 「TR」)找到(」 :輸入:沒有(.chkbx)「)'?或者'$ this.closest(「tr」)。find(「:input」)。not('。chkbx')'? –

回答

1

一個多行添加到您的現有代碼:

$(".chkbx").change(function(){ 
    var $this = $(this); 
    if ($this.is(":checked")) { 
     $this.closest("tr").find(":input").attr("disabled", false); 
    } else { 
     $this.closest("tr").find(":input").attr("disabled", true); 
    } 
    $this.attr("disabled",false); 
}); 

這不是最佳的或有效的,但它應該工作。

0

這個怎麼樣:

$(".chkbx").change(function(){ 
    var $this = $(this); 
    if ($this.is(":checked")) { 
     $this.closest("tr").find(":input").attr("disabled", false); 
    } else { 
     $this.closest("tr").find(":input:not(.chkbx)").attr("disabled", true); 
    } 
}); 

不知道這是否會工作!但試試吧!

4

我想用這樣的not功能會工作

$this.closest("tr").find(":input").not(".chkbx").attr("disabled", true); 
0

我建議像下面這樣:

$('.chkbx').change(
    function(){ 
     if ($(this).is(':checked')){ 
$(this).closest('tr').find('input,textarea,select').not($(this)).attr('disabled',true); 
     } 
     else { 
$(this).closest('tr').find('input,textarea,select').not($(this)).attr('disabled',false); 
     } 
    }); 
相關問題