首页 新闻 会员 周边 捐助

knockoutjs

0
[待解决问题]

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.

lucika.zh的主页 lucika.zh | 初学一级 | 园豆:54
提问于:2015-08-25 14:15
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册