Knockout.js 使用KnockoutJS将2个选择组合成1个选择
我的网页上有2个选择元素。有没有办法使用KnockoutJS将两个select元素中的select选项组合成一个select元素 给定:(2个选择)Knockout.js 使用KnockoutJS将2个选择组合成1个选择,knockout.js,Knockout.js,我的网页上有2个选择元素。有没有办法使用KnockoutJS将两个select元素中的select选项组合成一个select元素 给定:(2个选择) 一个 两个 三 四 所需结果: <select id="select1"> <option>Select 1</option> //if possible <option>One</option> <option>Two</option>
一个
两个
三
四
所需结果:
<select id="select1">
<option>Select 1</option> //if possible
<option>One</option>
<option>Two</option>
<option>Select 2</option> //if possible
<option>Three</option>
<option>Four</option>
</select>
如果可能,选择1//
一个
两个
如果可能,选择2//
三
四
在javascript中:
var select1 = $('#select1'),
select2 = $('#select2'),
// create an array to bind
options = ko.observableArray();
// this value doesn't exist in the dom
options.push('Select 1');
// add the text of all children to the array
select1.children().forEach(function(i, el) {
options.push(el.innerText);
});
// this value doesn't exist in the dom
options.push('Select 2');
// add the text of all children to the array
select2.children().forEach(function(i, el) {
options.push(el.innerText);
});
// bind an options with the options property
// set to the options array to the dom
ko.applyBindings({ options: options });
在HTML中:
<select data-bind="options: options"></select>
我不明白。你想要[a,b,c]在这两个列表中吗?我用插图更新了原始帖子为什么你需要KnockoutJS?
<select data-bind="options: options"></select>