2011-05-12 44 views
0

我有一個表單,其中包含多個複選框,如果選中某個方框,我想禁用表單。我將在我的網站上多次出現這種情況,我想知道是否有比我目前嘗試的更好的方式(哪個不能正常工作)。如果我想多次使用這個相同的禁用函數,我應該創建一個類'disable'嗎?謝謝!如何根據複選框選擇禁用表單

JS

$(document).ready(function() 
{ 
    $('#disabler_0').click(function() 
    { 

    $('[name*=form1]').attr('disabled', 'disabled') 
    }); 

}); 

HTML

<form id="form1" name="form1" method="post" action=""> 
     <p> 
     <label> 
      <input type="checkbox" name="disabler" value="one" id="disabler_0" /> 
      one</label> 
     <br /> 
     <label> 
      <input type="checkbox" name="disabler" value="two" id="disabler_1" /> 
      two</label> 
     <br /> 
     <label> 
      <input type="checkbox" name="disabler" value="three" id="disabler_2" /> 
      three</label> 
     <br /> 
     <label> 
      <input type="checkbox" name="disabler" value="four" id="disabler_3" /> 
      four</label> 
     <br /> 
     <label> 
      <input type="checkbox" name="disabler" value="five" id="disabler_4" /> 
      five</label> 
     <br /> 
     </p> 
    </form> 
+0

當你說禁用表單將如何顯示給用戶?你想要一個覆蓋?只需禁用所有的複選框? – 2011-05-12 13:57:16

+0

禁用所有複選框。 – dzilla 2011-05-12 13:59:12

回答

1

我無法禁用一個整體的形式,但每一個內它元素:

有一個虛擬更新例如這裏: http://jsfiddle.net/marcosfromero/S2SxN/

更新 的代碼應該適應您的需求,並把它更實用,因爲一旦你禁用了整個窗體,將無法再次啓用它:

$('#disabler_0').click(function() { 
    var elements = $(this).closest('form').find('input, select textarea').not('#disabler_0'); 
    if(this.checked) { 
     elements.attr('disabled', 'disabled'); 
    } else { 
     elements.removeAttr('disabled'); 
    } 
}); 
+0

爲你+1我的好人 – dzilla 2011-05-13 13:07:09

-1

這可以改良效果:

$(function() { 
    $('.disabler').click(function() { 
     $(this).parent('form').attr('disabled', 'disabled'); 
    }); 
}); 

這段代碼你不需要修改代碼爲每個表單

+0

您不能在表單上設置「disabled」屬性,它必須位於輸入上。 – 2011-05-12 14:01:31

+0

我知道,我只是嘗試添加一些改進 – 2011-05-12 14:04:01

0

你不能真正禁用一個表格中,disable屬性僅用於輸入。你想要做的是這樣的:

$("#form1").live("submit", function() { 

    if ($("#disabler_0").is(":checked")) 
     return false; 

    return true; 

}); 

這將不會讓表單提交時,複選框被選中。

編輯的評論澄清,這將禁用所有輸入並選擇複選框時選擇。

$('#disabler_0').live("click", function() { 

    var form = $(this).parents("form"); 
    var inputs = $("input,select,textarea", form); 
    inputs.attr("disabled", "disabled"); 
}); 
+0

也許我是用禁用的屬性措辭這個錯誤。我之前在某處寫過這段代碼,但我找不到它。如果選中「x」框,我想要什麼,其餘的框都是灰色的,無法檢查。也許這就是@onteria_的意思.. – dzilla 2011-05-12 14:02:23

-1

你可以把一個類上的所有複選框的要禁用窗體,並使用此代碼:

$('input.disabler').click(function() { 
    var form = $(this).closest('form'); 
    var isChecked = $(this).is(':checked'); 
    form.toggleClass('disabled', isChecked); 
    if (isChecked) 
     form.find(':input').attr('disabled', 'disabled') 
}); 

$('#form1').bind('submit', function() { 
    if ($(this).hasClass('disabled')) 
     return false; 
}); 
+0

你不能在表單上設置一個'disabled'屬性,它必須在輸入上。 – 2011-05-12 14:02:22

+0

@Nick這是真的,現在已經更新了。雖然它似乎仍然不是想要的東西。 – 2011-05-12 14:06:28

1

好吧,那麼,根據您的迴應:

$(document).ready(function() 
{ 
    $('#disabler_0').click(function() 
    { 
     $('#form1 input[type="checkbox"]').not("#disabler_0").attr('disabled', 'disabled') 
    }); 

}); 

這將禁用類型複選框的所有輸入元素,它們是窗體的子項。

+0

這很好,但是如何保持一個複選框以防萬一點擊?會切換而不是在那裏點擊工作? – dzilla 2011-05-12 14:12:10

+0

@dzilla我更新了響應以顯示如何使用.not()方法實現此目的,該方法從集合中刪除特定元素。 – 2011-05-12 14:15:08

相關問題