简体   繁体   中英

Disabling a checkbox in asp.net

I am dynamically creating a CheckBox and setting the disabled property like this:

chk.Disabled = false.

This renders the following HTML:

<input type="checkbox" disabled="disabled" .../>

On the click of the checkbox, I have a JS function that tries to enable it by doing a:

//get reference to the checkbox
chk.disabled = false;

This does not work. Any help?

如果您的复选框已禁用,则不会调用onclick

What you're trying to do is a bit odd. You're trying to enable a checkbox by clicking on the checkbox, which is disabled to start with. So, the onclick will not be registered until the checkbox is actually enabled.

Try the below to see what I mean.

<html>
<body>
    <input id="cb" type="checkbox" disabled="disabled" onclick="this.disabled=!this.disabled;" />
    <label for="cb">Click me</label>

    <input type="button" value="Click me instead!" onclick="cb.disabled=!cb.disabled;" />
</body>
</html>

Hope that helps you out.

How are you dynamically creating the check box?

Keep in mind that ASP.NET will change the name of the check box you give it, especially if you add it on the code behind.

What you need to do is send to your JS function the clientId, which is the id the HTML item will get when render.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM