gpt4 book ai didi

knockout.js - knockoutjs 'optionsValue' 不起作用

转载 作者:行者123 更新时间:2023-12-01 12:50:20 24 4
gpt4 key购买 nike

我有一个代码不起作用。这是这里 http://jsfiddle.net/JPBarbosa/uxwTM/4/ .

数据绑定(bind)参数 optionsValue 不像参数 optionsText 那样工作。

我对两者使用相同的功能!

<select data-bind="options: times, optionsText: function(item) { return item.toLocaleTimeString(); }, optionsValue: function(item) { return item.toLocaleTimeString(); }, value: selectedTime"></select>

问候,JP。

最佳答案

您为 optionsValue 传入的对象必须是与要用作值的属性名称相对应的字符串。您不能将它设置为这样的值,不幸的是,它的工作方式与 optionsText 不同。

times 数组映射到您想要的值会更容易。

<select data-bind="options: ko.utils.arrayMap(times(), function (time) { return time.toLocaleTimeString(); }), value: selectedTime"></select>

尽管您希望将映射代码隐藏在您的 View 之外。因此,向返回映射值的 View 模型添加一个计算可观察值。

var ViewModel = function() {
// ...
self.mappedTimes = ko.computed(function () {
return ko.utils.arrayMap(self.times(), function (time) {
return time.toLocaleTimeString();
});
})
};
<select data-bind="options: mappedTimes, value: selectedTime"></select>

Updated fiddle

关于knockout.js - knockoutjs 'optionsValue' 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12965164/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com