2009-10-07 12 views

答えて

47

チェックボックスでこれを入れて:

のjQueryを使用して
onclick="document.getElementById('IdOfTheTextbox').disabled=this.checked;" 
+11

+1 - 私は条件付きの不足が好きです。 –

-2

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

私は真剣にこの答えが3年間生き残っているとは信じられません...左側で '$(" textbox ")[0] .disabled'を使うか、行全体を' $ ( "#textbox")。prop( 'disabled'、...); ' - あ、なぜ、右側に' this.checked'を使わないのですか? – 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

次のように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()">

を動作するコードです。

関連する問題