是否可以使用值绑定将 Knockout 可观察属性绑定到单选按钮?
这就是我想要做的,但该值最终是字符串“[Object object]”而不是我的可观察属性的实际实例:
<input type="radio" name="vehicleGroup" data-bind="checked: vehicleGroupViewModel().selectedGroupOption , value:vehicleGroupViewModel().car" />
<input type="radio" name="vehicleGroup" data-bind="checked: vehicleGroupViewModel().selectedGroupOption , value:vehicleGroupViewModel().truck" />
这是我正在使用的视图模型:
var VehicleGroupViewModel = function(){
var self = this;
this.selectedVehicleGroup = ko.observable();
this.selectedGroupOption = ko.observable();
this.selectedGroupOption.subscribe(function (newVal) {
self.selectedVehicleGroup(newVal);
}
this.selectedGroup = ko.observable();
this.car = ko.observable(new VehicleViewModel());
this.truck = ko.observable(new VehicleViewModel());
}
var VehicleViewModel = function(){
this.name = ko.observable();
}
所以最后我希望汽车或卡车车辆 ViewModel 位于选定的可观察车辆组中。
据记录here只有 Select 节点能够将任意 JavaScript 对象绑定到值。其他输入需要字符串值,这就是您的值返回“[Object object]”的原因。
您仍然可以做您想做的事情,但您必须手动映射键并自己找到合适的对象。这是一个小提琴,演示了:
http://jsfiddle.net/jearles/JcPXy/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)