Objective
The value bindings are the values of the associated DOM elements to the properties of the view model. Mainly used on form controls <input>,<select> and <textarea>.
When the user edits the form control, the property values for the view model are automatically updated. Similarly, when you update the View model property, the corresponding element value is automatically updated on the page.
Note: If you use the checked binding on a CheckBox or radio button to read or write to the checked state of the element, instead of the value's binding.
Simple example
The code follows the property binding of value to two input
<p>login Name: <input data-bind= "Value:username"/></p><p>password: <input type= "Password "Data-bind=" Value:userpassword "/></p><script type=" Text/javascript "src=" ~/Scripts/ Knockout-2.3.0.debug.js "></script> <script type=" Text/javascript "> var viewModel = { UserName:ko.observable (""), userPassword:ko.observable ("abc") };
The post-run effect is
Note the type of the password is password
Ko sets this parameter to the value of the element. The previous value will be overwritten.
If the parameter is a monitoring attribute observable, the value of the element will be updated based on the value of the parameter, and if not, the value of the element will be set only once and not later.
If you provide a parameter that is not a number or a string (but an object or an array), the value shown is the content of yourparameter.tostring () (usually useless, so it is best to set it to a number or a string).
Whenever you update the value of an element, the KO will automatically update the attribute value corresponding to the view model. By default, KO updates this value when the user leaves the focus (for example, the onchange event), but you can specifically specify the time to change the value by using the 2nd parameter valueupdate.
Valueupdate
If you use the Valueupdate parameter, that means that KO will use the custom event instead of the default left focus event. Here are some of the most common options:
"Change" (default)-updates the value of the view model when the focus is lost, or when the <select> element is selected.
"KeyUp" – updates the view model as soon as the user finishes typing a character.
"KeyPress" – Update the view model immediately when the user is tapping a character but does not release the keyboard. Unlike KeyUp, this update is the same as KeyDown.
"Afterkeydown" – Updates the view model when the user starts typing characters. The main thing is to capture the browser's KeyDown event or asynchronous handle event.
With these options, using "Afterkeydown" is the best option if you want to update your view model in real time.
<p>your value: <input data-bind= "Value:somevalue, valueupdate: ' Afterkeydown '"/></p><p>you Has typed: <span data-bind= "Text:somevalue" ></span></p>
SomeValue:ko.observable ("Edit Me")
Binding drop-down menu Drop-down list (e.g. Select)
Knockout has a special support for the drop-down menu Drop-down list binding, that is, when reading and writing bindings, this value can be any JavaScript object without having to be a string. Useful when you let your users select a set of model objects. For specific examples, refer to options bindings.
Similarly, if you want to create a multi-select list, refer to selectedoptions bindings.
Update observable and Non-observable property values
If you use the value binding to associate your form element with your observable attribute, Ko sets the two-way binding of the 2-way, and any one change will update the value of the other party.
However, if your element is bound to a non-observable property (such as an original string or JavaScript expression), Ko will do so:
1. If you bind the Non-observable property as a simple object, such as a common property value, Ko sets the value to the initial value of the form element, and if you change the value of the form element, KO will re-write the value back to this property of view mode. But when this property changes itself, the element does not change (because it is not observable), so it is simply a 1-way binding.
2. If the Non-observable property you bind is a complex object, such as a complex JavaScript expression or sub-attribute, Ko will also set the value to be the initial value of the form element, but when the value of the form element is changed, KO will not write the view again. The model property, which is called one-time-only value setter, is not a true binding.
For example:
<p>first value: <input data-bind= "Value:firstvalue"/></p> <!--two-way binding--><p >second value: <input data-bind= "Value:secondvalue"/></p> <!--one-way binding--><p> Third value: <input data-bind= "Value:secondValue.length"/></p> <!--no binding--<script Type= "text/JavaScript" > var viewModel = { firstValue:ko.observable ("Hello"),//observable SecondValue: "Hello, again"//Not observable }; Ko.applybindings (ViewModel);</script>
Knockout.js website Learning (value binding)