0

嗨,我有以下 java 脚本函数:

function EnableDisableTextBox(chkBoxId, txtBoxId) {
    var isChk = document.getElementById(chkBoxId);
    document.getElementById(txtBoxId).disabled = !(isChk.checked);
}

当我试图调用上述函数时,通过单击复选框它无法按预期工作

<asp:CheckBox ID="chkBachelors"
      onclick="javascript:EnableDisableTextBox('chkBachelors','txtFirstDegree');"
      runat="server" Text='<%$Resources:Resource, FirstDegree %>' TextAlign="Left"/>

<asp:TextBox ID="txtFirstDegree" CssClass="form-text" runat="server" 
      MaxLength="250">&lt;/asp:TextBox>

预期结果(当用户单击chkBachelors复选框时):

if "chkBachelors" check box is checked 
    then enable "txtFirstDegree" text box
else 
    disable "txtFirstDegree" text box   

问题是什么以及如何解决?

4

2 回答 2

1
    <asp:CheckBox ID="chkBachelors"
              onclick="EnableDisableTextBox(this);"
              runat="server" Text='' TextAlign="Left"/>

        <asp:TextBox ID="txtFirstDegree" CssClass="form-text" runat="server" 
              MaxLength="250"></asp:TextBox>


<script language ="javascript" type="text/javascript">
       function EnableDisableTextBox(checkbox)
       {
           var txtBoxId= "<%=txtFirstDegree.ClientID%>";
           document.getElementById(txtBoxId).disabled = !(checkbox.checked);
       }
</script>
于 2012-04-11T06:03:16.167 回答
0

<%=chkBachelors.ClientID%> 和 <%=txtFirstDegree.ClientID%> 将给出 Asp.Net 控件的客户端 ID。您不需要显式传递它们

function EnableDisableTextBox() {
            var isChk = document.getElementById(<%=chkBachelors.ClientID%> );
            document.getElementById(<%=txtFirstDegree.ClientID%>).disabled = !(isChk.checked);
        }
于 2012-04-11T05:56:56.963 回答