javascript - Add disabled selected option in select menu via jQuery - Stack Overflow

We have a select menu that is being populated via data- attributes and a knockout.js array. I'm no

We have a select menu that is being populated via data- attributes and a knockout.js array. I'm not so familiar with jQuery as well, keep this in mind.

How could I add an option via jQuery that is the first selected one and it's disabled as well. Basically, it's being used as a placeholder. I know how to do this via HTML, but not in this way. Here is the HTML that is being used:

<select id="delemilter" data-bind="options: delimiterList, value: delimiterSelectedValue, optionsValue: 'value', optionsText: 'name', enable: true"></select>

In other select menus, one of our developers used this line of jQuery. Seems to be some knockout API stuff. Looks like an observable array:

self.selectMenuID.push({ value: 'Placeholder Text', key: 0, data: 0, disabled: true });

Can't figure out how to reuse this again... Hopefully this is enough information.

Thank you!

We have a select menu that is being populated via data- attributes and a knockout.js array. I'm not so familiar with jQuery as well, keep this in mind.

How could I add an option via jQuery that is the first selected one and it's disabled as well. Basically, it's being used as a placeholder. I know how to do this via HTML, but not in this way. Here is the HTML that is being used:

<select id="delemilter" data-bind="options: delimiterList, value: delimiterSelectedValue, optionsValue: 'value', optionsText: 'name', enable: true"></select>

In other select menus, one of our developers used this line of jQuery. Seems to be some knockout API stuff. Looks like an observable array:

self.selectMenuID.push({ value: 'Placeholder Text', key: 0, data: 0, disabled: true });

Can't figure out how to reuse this again... Hopefully this is enough information.

Thank you!

Share Improve this question edited Jan 26, 2015 at 22:02 Megaroeny asked Jan 26, 2015 at 21:49 MegaroenyMegaroeny 8273 gold badges14 silver badges27 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

You can do this as follows:

$('#delemilter').prepend('<option disabled="disabled">My disabled Option</option>');

Remember to specify other attributes in our option's html as required.

Here is a JSFiddle for you: http://jsfiddle/loanburger/ask9L71h/

I find this way much more elegant & readable:

$("select").append($("<option>", {
    value: "foo",
    text: "bar"
}).prop("disabled", true));

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745431628a4627417.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信