Knockout JS 选项数据绑定设置 select 为 "selected"

knockout JS option data-bind set select as "selected"

我有以下代码,想将一个选项设置为 "selected"。 通常我为此使用 "optionsValue",但它似乎在这里不起作用。

<select data-live-search="true" data-bind="foreach: $parent.customers, event: {change: $parent.changeWorkCustomer($data, $element.value)}">
    <option data-bind="text: customer_display_name, value: customer_id, attr: {'data-tokens': [customer_first_name(), customer_last_name(), css: { customer_fav: fav() > 0 }"></option>
</select>

关于如何将一个选项设置为选中的任何想法?

通过将 observable 绑定到 select 以保存选定的选项值。

<select data-live-search="true" data-bind="foreach: $parent.customers, 
                                           event: {change: $parent.changeWorkCustomer($data, $element.value)}, 
                                           value: selectedCustomer">
    <option data-bind="text: customer_display_name, value: customer_id, attr: {'data-tokens': [customer_first_name(), customer_last_name(), css: { customer_fav: fav() > 0 }"></option>
</select>

// selects the option whose value is customerId1
this.selectedCustomer = ko.observable('customerId1');