2012-10-28 17 views
0

我有一個帶有幾組單選按鈕的表單。選擇單選按鈕將啓用text字段,並禁用其餘字段。 我不能讓字段啓用。我已經嘗試了幾乎所有的功能(next,nextAll,nextUnit,find,findAll,close ...),但可能沒有正確使用它們。啓用/禁用字段的單選按鈕

這裏是只有一組按鈕的測試:http://jsfiddle.net/QTseK/

而且在頁面加載一些單選按鈕已經被檢查,我必須跑回去取剩下的(未選中的字段)禁用?

感謝

回答

1

當你的元素都有類屬性,你可以使用類選擇,啓用/禁用的表單元素選擇它們,你可以使用prop方法。

$("body").on("click", "input[type='radio']", function() { 
    $('.fieldSelector').prop('disabled', true); 
    $(this).closest('tr').find('.fieldSelector').prop('disabled', false) 
}); 

http://jsfiddle.net/AfRdj/

+0

另外'$( 「輸入[類型^ = '無線電']」)不( ':檢查')each'有關禁用字段。啓動。謝謝 –

0

jsFiddle

$("body").on("click", "input[type=radio]", function() { 
    $(this).closest("table").find("input[type=text]").prop("disabled", true); 
    $(this).closest("tr").find("input[type=text]").prop("disabled", false); 

});​ 

您可以添加/修改根據您的HTML中的選擇。

此外,我不確定我會將事件偵聽器放在主體上。我認爲如果元素在整個頁面生命週期中都在DOM中,我更喜歡這樣的東西。 (是否有問題的元件在網頁最初?)

$("input[type=radio]").on("click", function() {//you can change input[type=radio] to any other selector that you have already used 
    $(this).closest("table").find("input[type=text]").prop("disabled", true); 
    $(this).closest("tr").find("input[type=text]").prop("disabled", false); 

});​