0
我正在使用Knockout JqAutocomplete插件。当我从自动完成中选择一个项目时,我想为该选择选择一个ID和一个名称。 我知道JqAutocomplete文档就能解释得很好,但我尝试所有的变化,我无法得到它的工作,Knockout jqAutocomplete:如何选择Id和Name?
<table>
<tr>
<th>remote data</th>
</tr>
<tr>
<td>
<input data-bind="jqAuto: { source: remoteHandler, value: remoteValue, inputProp: remoteTest, labelProp: 'test', valueProp: 'val', options: { delay: 250 } }" />
</td>
</tr>
<tr>
<td>
<pre data-bind="text: ko.toJSON(remoteValue)"></pre>
<pre data-bind="text: ko.toJSON(remoteTest)"></pre>
</td>
</tr>
</table>
var ViewModel = function() {
this.remoteValue = ko.observable();
this.remoteTest = ko.observable();
this.remoteHandler = function(term, callback) {
setTimeout(function() {
callback([
{ test: term + "-1", val: 1 },
{ test: term + "-2", val: 2 },
{ test: term + "-3", val: 3 },
{ test: term + "-4", val: 4 },
{ test: term + "-5", val: 5 }
]);
}, 500);
}
};
ko.applyBindings(new ViewModel());
看到http://jsfiddle.net/arame3333/b0jqmmjt/1/
和https://github.com/rniemeyer/knockout-jqAutocomplete
我只想显示文本而不是Id。但是在底层视图模型中,我希望两者都得到更新。
如果删除了'valueProp'选项,整个项目被存储在'remoteValue'。要仅在UI中显示'text'道具,请使用'with:remoteValue'和'text:text'进行绑定。 http://jsfiddle.net/vzbr76yg/ – user3297291
谢谢。我尝试了各种各样的变化,但我没有想到那个。这是正确的答案。 – arame3333