禁用asp.net中的复选框

时间:2021-03-15 03:14:37

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

我动态创建一个CheckBox并设置disabled属性,如下所示:

chk.Disabled = false.

This renders the following HTML:

这将呈现以下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:

单击复选框,我有一个JS函数,尝试通过执行以下操作来启用它:

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

This does not work. Any help?

这不起作用。有帮助吗?

3 个解决方案

#1


If your checkbox is disabled, your onclick wont be called

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

#2


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.

你要做的事情有点奇怪。您正尝试通过单击复选框启用复选框,该复选框已禁用以启动。因此,在实际启用该复选框之前,不会注册onclick。

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.

希望能帮到你。

#3


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.

请记住,ASP.NET将更改您提供的复选框的名称,尤其是在后面的代码中添加它时。

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

你需要做的是向你的JS函数发送clientId,它是HTML项目在渲染时得到的id。

#1


If your checkbox is disabled, your onclick wont be called

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

#2


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.

你要做的事情有点奇怪。您正尝试通过单击复选框启用复选框,该复选框已禁用以启动。因此,在实际启用该复选框之前,不会注册onclick。

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.

希望能帮到你。

#3


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.

请记住,ASP.NET将更改您提供的复选框的名称,尤其是在后面的代码中添加它时。

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

你需要做的是向你的JS函数发送clientId,它是HTML项目在渲染时得到的id。