javascript 在检查更改时禁用文本框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7521959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disable textboxes on check changed
提问by l--''''''---------''''''''''''
I have a checkbox that disables 2 textboxes when checked. When it is unchecked it enables the checkboxes. Here is the JavaScript:
我有一个复选框,选中时会禁用 2 个文本框。当它被取消选中时,它会启用复选框。这是 JavaScript:
function enableField() {
prePracticeCodeTextBox = document.getElementById('prePracticeCodeTextBox');
preContactTextBox = document.getElementById('preContactTextBox');
checkTheBox = document.getElementById('CheckBox1');
if (checkTheBox.checked == true) {
prePracticeCodeTextBox.disabled = false;
preContactTextBox.disabled = false;
}
else {
prePracticeCodeTextBox.disabled = true;
preContactTextBox.disabled = true;
}
}
Here is the HTML:
这是 HTML:
<dl>
<dt><label for="CheckBox1">PreAnalytical?</label></dt>
<dd> <asp:CheckBox ID="CheckBox1" runat="server" CausesValidation="false"
Visible="true" OnCheckChanged="enableField()"/></dd>
</dl>
<dl>
<dt><label for="prePracticeCodeTextBox">Practice Code:</label></dt>
<dd><asp:TextBox ID="prePracticeCodeTextBox" runat="server" Enabled="False" /></dd>
</dl>
<dl>
<dt><label for="preContactTextBox">Contact:</label></dt>
<dd><asp:TextBox ID="preContactTextBox" runat="server" Enabled="False" /></dd>
</dl>
The JavaScript function is not being called at all.
JavaScript 函数根本没有被调用。
What am I doing wrong?
我究竟做错了什么?
回答by tugberk
Try to use onclick instead. Use the following code to register it on your code behind :
尝试使用 onclick 代替。使用以下代码将其注册到您的代码后面:
CheckBox1.Attributes.Add("onclick", "enableField();");
BTW, you won't be able to reach the elements as you do on asp.net web forms application with default settings. You need to get the ClientIDs of the elements which will be rendered :
顺便说一句,您将无法像在具有默认设置的 asp.net Web 表单应用程序上那样访问元素。您需要获取将要呈现的元素的 ClientID:
function enableField() {
prePracticeCodeTextBox = document.getElementById('<%=prePracticeCodeTextBox.ClientID%>');
preContactTextBox = document.getElementById('<%=preContactTextBox.ClientID%>');
checkTheBox = document.getElementById('<%=CheckBox1.ClientID%>');
if (checkTheBox.checked == true) {
prePracticeCodeTextBox.disabled = false;
preContactTextBox.disabled = false;
}
else {
prePracticeCodeTextBox.disabled = true;
preContactTextBox.disabled = true;
}
}
If you are developing on .net 4, read the below article :
如果您在 .net 4 上开发,请阅读以下文章:
回答by Hanlet Esca?o
Change OnCheckChanged="enableField()
to onclick="enableField();"
. You are using server controls. OnCheckChanged is an event for the asp.net CheckBox control.
更改OnCheckChanged="enableField()
为onclick="enableField();"
。您正在使用服务器控件。OnCheckChanged 是 asp.net CheckBox 控件的事件。
回答by tareq marji
function enableField() {
函数 enableField() {
prePracticeCodeTextBox =.getElementById('<%=prePracticeCodeTextBox.ClientID%>');
preContactTextBox = document.getElementById('<%=preContactTextBox.ClientID%>');
checkTheBox = document.getElementById('<%=CheckBox1.ClientID%>');
if (checkTheBox.checked == true) {
prePracticeCodeTextBox.disabled = false;
preContactTextBox.disabled = false;
}
else {
prePracticeCodeTextBox.disabled = true;
preContactTextBox.disabled = true;
}
}
}