开发者

How do i add disabled=disabled attribute to <select> using jQuery?

开发者 https://www.devze.com 2023-03-23 07:42 出处:网络
I have a htmlfield. <select name=\"select-states\" disabled=\"disabled\"><option value=\"\">Choose State &raquo;</option></select>

I have a html field.

<select name="select-states" disabled="disabled"><option value="">Choose State &raquo;</option></select>

initially the field is disabled, i want to enable or disable it based on events. to enable it i am using the following jQuery which works fine.

$('select[name="select-states"]').removeAttr('disabled');

I wan开发者_StackOverflow中文版t to re-enable it using jQuery, for which i tried using this which does not work.

$('select[name="select-states"]').attr('disabled');

What am i missing? which is the correct syntax for this?

thank you


In your code, you provide only one parameter to .attr(). This indicates that the function should just return the value for the disabled attribute of that element.

To set the disabled attribute, provide the attribute name (first parameter) along with a value (second parameter):

$('select[name="select-states"]').attr('disabled', 'disabled');
0

精彩评论

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