2009-10-07 159 views

回答

47

把這個:

onclick="document.getElementById('IdOfTheTextbox').disabled=this.checked;" 
+11

+1 - 我喜歡缺乏條件。 – 2009-10-07 13:15:10

-2

使用jQuery:

$("#checkbox").click(function(){ 
    $("#textbox")[0].disabled = $(this).is(":checked"); 
}); 
+0

我真的不能相信這個答案存活了三年,而沒有工作......可以在左邊使用'$(「#textbox」)[0] .disabled'或者用'$替換整行(「#textbox」)。prop('disabled',...);' - 哦,爲什麼不在右側使用'this.checked'? – ThiefMaster 2012-09-11 22:28:55

14
<input type="text" id="textBox"> 
    <input type="checkbox" id="checkBox" onclick="enableDisable(this.checked, 'textBox')"> 
    <script language="javascript"> 
    function enableDisable(bEnable, textBoxID) 
    { 
     document.getElementById(textBoxID).disabled = !bEnable 
    } 
</script> 
2

創建一個JavaScript函數是這樣的:

function EnableTextbox(ObjChkId,ObjTxtId) 
{ 

    if(document.getElementById(ObjChkId).checked) 
     document.getElementById(ObjTxtId).disabled = false; 
    else 
     document.getElementById(ObjTxtId).disabled = true; 
} 

像這樣創建一個C#函數在網格RowDataBound上:

protected void lstGrid_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     TextBox txtAllowed = (TextBox)e.Row.FindControl("txtAllowed"); 

     CheckBox chkAllowed = (CheckBox)e.Row.FindControl("RowSelector"); 
     chkAllowed.Attributes.Add("onClick", "EnableTextbox('" + chkAllowed.ClientID + "', '" + txtAllowed.ClientID + "')"); 
    } 
} 
4
jQuery(document).ready(function() { 
    $("#checkBox").click(function() { 
     $('#textBox').attr("disabled", $(this).is(":checked")); 
    }); 
}); 
0
<script type="text/javascript"> 
    function EnableDisableTextBox(chkPassport) { 
     var txtPassportNumber = document.getElementById("txtPassportNumber"); 
     txtPassportNumber.disabled = chkPassport.checked ? false : true; 
     if (!txtPassportNumber.disabled) { 
      txtPassportNumber.focus(); 
     } 
    } 
</script> 
<label for="chkPassport"> 
<input type="checkbox" id="chkPassport" onclick="EnableDisableTextBox(this)" /> 
Do you have Passport? 
</label> 
<br /> 
Passport Number: 
<input type="text" id="txtPassportNumber" disabled="disabled" /> 
0

我對這個簡單的任務,但最簡單的解決方案。 相信我還是沒有它的工作原理

s = 1; 
 
    function check(){ 
 
     o = document.getElementById('opt'); 
 
     if(o.value=='Y'){ 
 
      s++; 
 
      if(s%2==0) 
 
      $('#txt').prop('disabled',true); 
 
      else 
 
      $('#txt').prop('disabled',false); 
 
     } 
 
     
 
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
Text: <input type="text" name="txt" id="txt"> 
 
<input type="checkbox" name="opt" id="opt" value="Y" onclick="check()">

這裏是代碼。

相關問題