简体   繁体   English

淘汰表选择数据绑定绑定得到意外结果

[英]knockoutjs select data-bind binding is given unexpected result

I am just started to use knockoutjs. 我才刚开始使用淘汰表。 I try to bind the select option value with knockout data-bind property.But am not able to get the array values as different options. 我尝试将选择选项值与敲除数据绑定属性绑定在一起,但是无法将数组值作为不同的选项获取。 It will populate as coma separetd. 它将填充为昏迷状态。 i had attaching the sample code i tryied.I hope some others will also face the same strange error. 我已经附加了我尝试过的示例代码。我希望其他一些人也会遇到同样的奇怪错误。

self.availableStates = new Array(); 
for (var i=0;i<self.allStates.length;i++)
{
if (self.allStates[i]['name'] != null)
self.availableStates.push(self.allStates[i]['name'])
}
self.availableStates = ko.observableArray([self.availableStates]);

Expecting result: 预期结果:

<select id="drpDwnLst" data-bind="options: availableStates">
    <option value="">State4</option>
    <option value="">State3</option>
    <option value="">State2</option>
    <option value="">State1</option>
</select>

Actual result 实际结果

<select id="drpDwnLst" data-bind="options: availableStates">
    <option value="">State4,State3,State2,State1</option>
</select>

What you see in the output is the effect of calling toString() on an array. 您在输出中看到的是在数组上调用toString()的效果。

You get this result because your availableStates is already an array so you don't need to wrap it again to an array with [] 之所以会得到这个结果,是因为您的availableStates 已经是一个数组,因此您无需再次使用[]将其包装到一个数组中

So you need to just write: 所以你只需要写:

self.availableStates = ko.observableArray(self.availableStates);

Although it is strange how you override your self.availableStates definition... 虽然很奇怪您如何覆盖self.availableStates定义...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM