[英]knockout js selected option
I have a select tag in my html with knockout js binding. 我在我的HTML中有一个带有剔除js绑定的选择标签。
<select class="form-control " data-bind="options: loanTimesBorrower,
optionsText: loanTimesBorrower(),
value: loanTimeBorrower,
selectedOptions: loanTimesBorrowerini,
optionsCaption: 'Choose dates'">
</select>
Instead of choose date I want to show the default value as 180, since I have a array which stores this loantime as 180 days, 360 days etc. 我希望将默认值显示为180,而不是选择日期,因为我有一个存储此借贷时间为180天,360天等的数组。
this is the array 这是数组
self.loanTimesBorrower = ko.observableArray();
self.loanTimesBorrowerini = ko.observableArray(self.loanTimesBorrower()[0]);
and I get this array populated by a foreach loop which is getting the loantimes from db like this. 并且我得到了一个由foreach循环填充的数组,该循环正在像这样从db获取借用时间。
$.each(items.investTimes, function (index, item) {
self.loanTimesBorrower.push(item.Loantime);
});
So I am not sure how the default value can be put as 180 instead of choose dates 所以我不确定如何将默认值设置为180而不是选择日期
删除optionsCaption: 'Choose dates'
它将默认为您提供的选项。
You can use it like this: 您可以像这样使用它:
<select id="selectDate" class="form-control " data-bind="options: loanTimesBorrower,
optionsText: loanTimesBorrower(),
value: loanTimeBorrower,
selectedOptions: loanTimeBorrower[0],
optionsAfterRender: $root.setDefaultDate">
</select>
And in your javascript you can do something like: 在您的JavaScript中,您可以执行以下操作:
self.setDefaultDate= function (option, object) {
if (typeof object !== "undefined" && object !== null) {
if(object.value === 180) {
$("#selectDate").find(option).prop('selected', true);
}
}
};
if you show me what the object in your array looks like I can edit the answer for a better fit. 如果您向我展示数组中对象的外观,则可以编辑答案以使其更合适。 But that should help you to find your way 但这应该可以帮助您找到自己的出路
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.