2009-10-07 19 views
8

क्या कोई मुझे बता सकता है कि कोई टेक्स्टबॉक्स अक्षम करने के लिए, यदि कोई चेकबॉक्स चेक किया गया है, और चेकबॉक्स चेक नहीं किया गया है तो टेक्स्टबॉक्स सक्षम करें?चेकबॉक्स चेक किए गए चेकबॉक्स के आधार पर टेक्स्टबॉक्स को अक्षम कैसे करें

उत्तर

47

चेकबॉक्स में रखें यह:

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

+1 - मुझे सशर्त की कमी पसंद है। –

-2

jQuery का उपयोग करना:

function EnableTextbox(ObjChkId,ObjTxtId) 
{ 

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

इस तरह एक सी # समारोह बनाएँ:

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

मैं गंभीरता से विश्वास नहीं कर सकता कि यह उत्तर तीन साल तक जीवित रहा है जबकि काम नहीं कर रहा है ... या तो '$ (" # टेक्स्टबॉक्स ") [0]। अक्षम 'बाईं ओर' या पूरी लाइन को' $ के साथ बदलें ("#textbox")। prop ('अक्षम', ...); '- ओह, और दाहिने तरफ' 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

इस तरह एक जावास्क्रिप्ट समारोह बनाएं ग्रिड पर 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()">

काम करता है यहाँ कोड है।

संबंधित मुद्दे