如果选中复选框,是否有人可以告诉我如何禁用文本框?如果未选中复选框,是否可以启用文本框?
答案 0 :(得分:48)
将其放在复选框中:
onclick="document.getElementById('IdOfTheTextbox').disabled=this.checked;"
答案 1 :(得分:16)
<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 :(得分:4)
jQuery(document).ready(function () {
$("#checkBox").click(function () {
$('#textBox').attr("disabled", $(this).is(":checked"));
});
});
答案 3 :(得分:2)
创建一个这样的Javascript函数:
function EnableTextbox(ObjChkId,ObjTxtId)
{
if(document.getElementById(ObjChkId).checked)
document.getElementById(ObjTxtId).disabled = false;
else
document.getElementById(ObjTxtId).disabled = true;
}
在网格RowDataBound:
上创建这样的C#函数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 :(得分:1)
对于这个简单的任务,我有最简单的解决方案。 信不信由你,
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);
}
}
&#13;
<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()">
&#13;
这是代码。
答案 5 :(得分: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" />
答案 6 :(得分:-2)
使用jQuery:
$("#checkbox").click(function(){
$("#textbox")[0].disabled = $(this).is(":checked");
});