最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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));
发布评论

评论列表(0)

  1. 暂无评论