简体   繁体   English

敲除->将元素绑定到选择值

[英]Knockout -> Bind element to Value of Select

In the following select , I'm binding an observable array. 在下面的select ,我绑定了一个可观察数组。

<select id="selectProtocols" data-bind="options: optionsAvailable, optionsCaption:'Selecione...', optionsText: 'SimpleDescription', optionsValue:'???'"></select>

I believe it's easy to see that that the text of each option is a property "SimpleDescription", that works fine. 我相信很容易看到每个选项的文本都是属性“ SimpleDescription”,可以正常工作。 What I need now is that the value of the select is the element itself! 我现在需要的是selectvalue就是元素本身!

How can I do that? 我怎样才能做到这一点?

Note 2: Working with drop-down lists (ie, <select> elements) 注意2:使用下拉列表(即<select>元素)

Knockout has special support for drop-down lists (ie, <select> elements). 淘汰赛对下拉列表(即<select>元素)具有特殊支持。 The value binding works in conjunction with the options binding to let you read and write values that are arbitrary JavaScript objects, not just string values. 值绑定与选项绑定结合使用,使您可以读写任意JavaScript对象的值,而不仅仅是字符串值。 This is very useful if you want to let the user select from a set of model objects. 如果要让用户从一组模型对象中进行选择,这将非常有用。 For examples of this, see the options binding or for handling multi-select lists, see the documentation for the selectedOptions binding. 有关此示例,请参阅选项绑定或有关处理多选列表的信息,请参见selectedOptions绑定的文档。

http://knockoutjs.com/documentation/value-binding.html http://knockoutjs.com/documentation/value-binding.html

Simply do not set the optionsValue attribute and Knockout will automatically store the selected object from the observableArray into the observable you have declared in the value attribute. 只需不设置optionsValue属性,并且Knockout会将选中的对象从observableArray自动存储到您在value属性中声明的可观察对象中。

Deep within the bowels of Knockout is the following function: 以下功能位于淘汰赛的肠道深处:

function applyToObject(object, predicate, defaultValue) {
    var predicateType = typeof predicate;
    if (predicateType == "function")    // Given a function; run it against the data value
        return predicate(object);
    else if (predicateType == "string") // Given a string; treat it as a property name on the data value
        return object[predicate];
    else                                // Given no optionsText arg; use the data value itself
        return defaultValue;
}

If you do not set the optionsValue attribute then this function will fall into the bottom else statement which will return the current array item. 如果未设置optionsValue属性,则此函数将落在底部的else语句中,该语句将返回当前数组项。

Assigning $data to optionsValue does not make sense here, you have to read what optionsValue does [1]. 在这里,将$data分配给optionsValue没有意义,您必须阅读optionsValue功能[1]。 It basically just sets the value attribute of the <option> elements under your <select> . 基本上,它只是在<select>下设置<option>元素的value属性。 So you can't set it to the JS object that's being selected, you can only set it to text. 因此,不能将其设置为所选的JS对象,而只能将其设置为文本。

<select data-bind="
    options: optionsAvailable,
    optionsCaption: 'Selecione...',
    optionsText: 'SimpleDescription',
    optionsValue: 'SomeOtherPropertyThatEvaluatesToText',    
    value: selectedOption"></select>

The value binding should receive an observable that will be set to the selected JS object. value绑定应收到一个可观察到的对象,该对象将被设置为所选JS对象。 And, if you want, you can give optionsValue a function that takes this JS object and turns it into text, like this: 而且,如果需要,可以给optionsValue一个函数,该函数接受此JS对象并将其转换为文本,如下所示:

<select data-bind="
    options: optionsAvailable,
    optionsCaption: 'Selecione...',
    optionsText: 'SimpleDescription',
    optionsValue: JSON.stringify,    
    value: selectedOption"></select>

Here's a sample fiddle to play with: http://jsfiddle.net/43t9dzxt/1/ 这是一个可玩的小提琴示例: http : //jsfiddle.net/43t9dzxt/1/

[1] http://knockoutjs.com/documentation/options-binding.html [1] http://knockoutjs.com/documentation/options-binding.html

我最终使用了optionsValue:$dataoptionsAfterRender函数相结合...它可以像预期的那样在“自然”中传递对象!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM