禁用除一个之外的所有表单元素

时间:2011-11-30 02:01:48

标签: jquery

我使用以下命令禁用表格行中的所有表单元素:

$(".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)始终启用的复选框。如何将其从函数中排除?

4 个答案:

答案 0 :(得分:4)

我认为使用这样的not函数可以正常工作

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

答案 1 :(得分: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);
});

这不是最佳或有效的,但应该有效。

答案 2 :(得分: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);
    }
});

不确定它是否会起作用!但试一试!

答案 3 :(得分: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);
        }
    });