Javascript 级联select2组合框和Knockout JS
代码如下: combobox脚本包含在底部,如果删除它,一切正常 我似乎无法让它用淘汰Ajax提供给我的新内容刷新组合框 HTMLJavascript 级联select2组合框和Knockout JS,javascript,jquery,knockout.js,combobox,Javascript,Jquery,Knockout.js,Combobox,代码如下: combobox脚本包含在底部,如果删除它,一切正常 我似乎无法让它用淘汰Ajax提供给我的新内容刷新组合框 HTML <div> <div class="cbp-content" id="didScreen"> <div> <table width='100%'> <thead>
<div>
<div class="cbp-content" id="didScreen">
<div>
<table width='100%'>
<thead>
<tr>
<th width='25%'>State</th>
<th width='25%'>City</th>
<th class='quantity' width='10%'>Quantity</th>
<th width='10%'></th>
</tr>
</thead>
<tbody data-bind="foreach: items">
<tr>
<td>
<select class="combobox" data-bind="options: $parent.states, optionsText: 'name', value: state, optionsCaption: 'Select State'"></select>
</td>
<td>
<select class="combobox" data-bind="options: cities, optionsText: 'rc_abbr', optionsValue: 'id', value: city"></select>
</td>
<td>
<input name="quantity" data-bind="value: quantity" />
</td>
<td>
<button class="btn" data-bind="click: $parent.remove"><i class="icon-remove"></i>
</button>
</td>
</tr>
</tbody>
</table>
</div>
<button class="btn" data-bind="click: newItem">Add Item</button>
</div>
</div>
</fieldset>
您必须在创建的所有新组合框上再次调用
。选择2()。现在只在DOM就绪时存在的组合框上调用它
您可以更改此选项:
self.newItem = function () {
self.items.push(new Item());
};
为此:
self.newItem = function () {
self.items.push(new Item());
$(".combobox").not('.select2-container').select2();
};
感谢您的输入,newItem的工作方式是复制级联选择。因此,再次这样称呼它意味着他们必须点击“添加更多”,然后将城市放入。但是这方面的逻辑是好的,我这样尝试:$(“.combobox”).on(“change”,function(){$(“.combobox”).select2();})代码>但是它仍然不起作用,还有其他建议吗?我还尝试将代码放入ajax complete函数中,但它仍然没有刷新组合框。嗯,我不确定我是否得到了您想要的结果,抱歉。
self.newItem = function () {
self.items.push(new Item());
$(".combobox").not('.select2-container').select2();
};