How to disable TextBox on the client side, click on CheckBox

How to disable the asp:TextBoxon client-sideclick HTML checkboxor on the server side asp:CheckBoxusing JavaScript?

<script type="text/javascript" language="javascript">
    function enableTextbox() {
        // ?
    }
</script>

<table>
    <tr>
        <td>
            <input id="checkTake" onclick="enableTextbox" title="Take?" />
        </td>
    </tr>
    <tr>
        <td>
            <asp:TextBox runat="server" ID="txtCommission" />
        </td>
    </tr>
</table>
+3
source share
3 answers
<script type="text/javascript" language="javascript">
    function enableTextbox(checkbox) {
        document.getElementById('<%= txtCommission.ClientID %>').disabled = !document.getElementById(checkbox).checked;
    }
</script>

<table>
    <tr>
        <asp:CheckBox runat="server" ID="checkTake" onclick="enableTextbox(this.id)" Checked="true" Text="Take?" />
    </tr>
    <tr>
        <td>
            <asp:TextBox runat="server" ID="txtCommission" MaxLength="8" CssClass="right" />
        </td>
    </tr>
</table>
+2
source

The hard part here is that ASP.NET assigns auto-generated attributes idto all elements runat="server", including yours TextBox. A simple solution is to "paste" the generated idin the script:

function enableTextbox() {
   var txtCommision = document.getElementById("<%= txtCommision.ClientID %>");
   txtCommision.disabled = !this.checked;
}

" " - , , JavaScript - id <input>. class. - , JavaScript, jQuery .

+3
function enableTextbox() {
  document.getElementById("<%= txtCommision.ClientID %>").disabled = !document.getElementById("checkTake").checked;
};

You also need to call enableTextbox:

<input id="checkTake" onclick="enableTextbox()" title="Take?" />

JQuery

  $(document).ready(function () {
    $('#checkTake').bind('click', function () {
      $('#<%= txtCommission.ClientId %>').attr('disabled', !$(this).attr('checked'));
    });
  });
+1
source

Source: https://habr.com/ru/post/1739718/


All Articles