How to swap a disabled option DOM element to enabled?

半城伤御伤魂 提交于 2019-12-25 16:57:29

问题


I have a list with enabled and disabled option. I do know how to disable an option element but what I don't know how to enable it again.

<select size="1" id="x">
  <option value="47" disabled="disabled">Value 47</option>
  ...


selectElement.options[i].disabled = 'disabled';
// ... how to enable?

It should be done with Plain Javascript and no JavaScript Framework. (I wish I could use Prototype or a similar framework but I cannot introduce one of them.)


回答1:


Use setAttribute and removeAttribute:

selectElement.options[i].setAttribute("disabled", "disabled");
selectElement.options[i].removeAttribute("disabled");



回答2:


The DOM object's property is a boolean value, that should be set to true or false:

selectElement.options[i].disabled = false;

Also see Boolean HTML Attributes.



来源:https://stackoverflow.com/questions/4315197/how-to-swap-a-disabled-option-dom-element-to-enabled

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!