knockout 对于checxbox可以使用数组控制是否选中。
<p>Send me spam: <input type="checkbox" data-bind="checked: wantsSpam" /></p> <div data-bind="visible: wantsSpam"> Preferred flavors of spam: <div><input type="checkbox" value="cherry" data-bind="checked: spamFlavors" /> Cherry</div> <div><input type="checkbox" value="almond" data-bind="checked: spamFlavors" /> Almond</div> <div><input type="checkbox" value="msg" data-bind="checked: spamFlavors" /> Monosodium Glutamate</div> </div> <script type="text/javascript"> var viewModel = { wantsSpam: ko.observable(true), spamFlavors: ko.observableArray(["cherry","almond"]) // Initially checks the Cherry and Almond checkboxes }; // ... then later ... viewModel.spamFlavors.push("msg"); // Now additionally checks the Monosodium Glutamate checkbox </script>
对于其他绑定,有没有这样的效果?
如 hasFocus
如果对于value、text有这样的实现会更好,就可以避免声明多个变量。
如:
The item is <span data-bind=
"text: priceRating1"
></span> today.
The item is <span data-bind=
"text: priceRating2"
></span> today.
The item is <span data-bind=
"text: priceRating3"
></span> today.