使用对复杂对象的剔除为选择框手动设置默认值 [英] Manually set default values for a select box using knockout on complex objects

查看:86
本文介绍了使用对复杂对象的剔除为选择框手动设置默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用基因剔除来更新选择框之一中的所选值.但是我不能这样做.这是jsfiddle链接- http://jsfiddle.net/5MauG/1/

I am trying to update the selected value in one of the select boxes using knockout. But I am not able to do so. Here is the jsfiddle link - http://jsfiddle.net/5MauG/1/

当我单击单击我"范围时,我希望选择框中的所选值应该更改.

When I click on the click me span, I expect that the selected value in the select box should change.

推荐答案

您的问题是,尝试将所选选项设置为新对象是行不通的.即使具有相同的值,新对象也不会与旧对象相同.您可以在此修饰的小提琴中看到它;实际上是从options数组中的对象中选择时有效的.

You're problem is that trying to set the selected option to a new object won't work. Even with the same values, the new object is not the same as the old object. You can see that in this slightly modified fiddle; it works when actually choosing from the objects in the options array.

使用值数组(例如字符串或整数),可以按值选择.对于对象数组,您需要选择具有实际对象的对象.如果您的视图在各处都使用绑定,这将变得更加容易,因为绑定代表实际的对象.

With an array of values, like strings or ints, you can select by value. With an array of objects, you need to select with the actual object. This will be easier if your view is using the bindings everywhere, because the bindings will represent the actual objects.

这篇关于使用对复杂对象的剔除为选择框手动设置默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆