2012-06-26 35 views
5

我正在學習jQuery,並在添加和刪除HTML複選框中禁用的屬性的一小會兒。我想這樣做,所以當你檢查框時,你不能在一個文本區寫一個地址,否則你必須從搜索框中選擇。我有後來完成,但複選框正在造成我的麻煩。這是我的函數調用。它進入函數就好了,但在檢查時不會刪除禁用的屬性,並引發錯誤,它無法使用addAttr方法添加禁用的後退。使用jQuery從html複選框中刪除和添加禁用的屬性

//attach an event for clicking on the informal contact button 
jQuery(document).ready(
    function() { 
     jQuery('.InformalContact').live('click', 
     function (event) { 
      TestIfInformalContactIsChecked(event); 
     }); 
    } 
); 

//check the status of the informalcontact checkbox when a user activates it 
//If checked, user can input data in the contactinfo manually. 
function TestIfInformalContactIsChecked(event) { 
    var thisCheck = jQuery(event.target); 
    if (thisCheck.is(':checked')) { 
     jQuery("#ContactInfo").removeAttr('disabled'); 
    } 
    else { 
     jQuery("#ContactInfo").addAttr('disabled');  
    } 
} 

這是HTML ...

<div class="TBItemColumn1"> 
    Name: <input id="Name" value="" class="TBFindContact" style="width: 150px;" maxlength="50" type="text" /><br /> 
    <input id="InformalContact" class="InformalContact" maxlength="50" type="checkbox" ClientIDMode="Static" />Informal Contact<br /> 
    <div id="TBContactSearchBox"></div> 
    Notes: 
    <br /> 
    <textarea id="Notes" style="width: 280px; height: 20px;"></textarea> 
    </div> 
    <div class="TBItemColumn2"> 
    <div class="FloatLeft"> 
     Contact Info: 
     <br /> 
     <textarea id="ContactInfo" value="" style="width: 280px; height: 70px;" disabled="disabled"></textarea> 
    </div> 
    </div> 

什麼我搞亂了設置禁用屬性上的複選框?

回答

8

爲了能夠使用該

jQuery("#ContactInfo").prop('disabled', true); 

這禁用

jQuery("#ContactInfo").prop('disabled', false); 

如果你刪除一個原生屬性爲disabled使用removeAttr(或removeProp),您將無法再次添加。它不應該用於更改屬性值。

相關問題