2009-10-07 15 views

Odpowiedz

47

umieścić to w polu wyboru:

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

+1 - Podoba mi się brak warunkowego. –

-2

jQuery:

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

Poważnie nie mogę uwierzyć, że ta odpowiedź przetrwała trzy lata, podczas gdy w ogóle nie działa ... Albo użyj '$ (" # textbox ") [0] .disabled' po lewej stronie, albo zamień całą linię na' $ ("#textbox"). prop ("wyłączone", ...); '- oh, i dlaczego nie używać' this.checked' po prawej stronie? – ThiefMaster

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

Tworzenie funkcji JavaScript takiego:

function EnableTextbox(ObjChkId,ObjTxtId) 
{ 

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

utworzyć funkcję C# tak w sieci 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

mam jeszcze najprostsze rozwiązanie tego prostego zadania. Wierzcie mi lub nie działa

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()">

Oto kod.