开发者

Disable/Enable HtmlSelect based on checkbox checked in javascript and asp.net

开发者 https://www.devze.com 2023-01-24 21:50 出处:网络
Here is the situation. I have a htmlselect control that when loaded needs to be initially enabled or disabled based upon something that is true or not from the database query. Then I need the user to

Here is the situation. I have a htmlselect control that when loaded needs to be initially enabled or disabled based upon something that is true or not from the database query. Then I need the user to be able to click on a checkbox to disable or enable the same htmlselect control, ALL without a postback.

Is 开发者_StackOverflow社区this possible?


Should be something like that:

 <input type="checkbox" onclick="document.getElementById('idOfSelect').disabled=(this.checked)?false:true">


You will have to generate the HTML dynamically at runtime, either by generating it from your server code or using a template. Use the code Dr.Molle suggested above, and insert an enabled attribute for the checkbox / disabled style for the SELECT based on the result from the database query:

<INPUT type-"checkbox" checked ...>
<SELECT ...>

if it is supposed to be enabled or

<INPUT type-"checkbox" ...>
<SELECT disabled ...>

if it is not.

Unfortunately, you can't combine the HTML generation to set the initial state and the dynamic change based on user interaction, unless you use a really sophisticated web framework.

Putting it all together, using a common templating convention, your HTML template might look like this:

<INPUT type="checkbox" ${mySelectIsInitallyEnabled ? "checked" : ""} onclick="document.getElementById('mySelect').disabled=(this.checked)?false:true">
<SELECT id="mySelect" ${mySelectIsInitallyEnabled ? "" : "disabled"}>

    <!-- options ... -->

</SELECT>


Using javascript and code-behind I was able to get this to work. Thank you all

0

精彩评论

暂无评论...
验证码 换一张
取 消