无线电绑定在knockoutjs教程中无法正常工作

时间:2012-05-26 18:23:14

标签: javascript knockout.js

我尝试只是演示来自knockout.js教程的单选按钮绑定,但它不能正常工作。例如,注释说应该检查msg值,但不是。

文档链接在这里http://knockoutjs.com/documentation/checked-binding.html

<p>Send me spam: <input type="checkbox" data-bind="checked: wantsSpam" /></p>
<div data-bind="visible: wantsSpam">
    Preferred flavor of spam:
    <div><input type="radio" name="flavorGroup" value="cherry" data-bind="checked: spamFlavor" /> Cherry</div>
    <div><input type="radio" name="flavorGroup" value="almond" data-bind="checked: spamFlavor" /> Almond</div>
    <div><input type="radio" name="flavorGroup" value="msg" data-bind="checked: spamFlavor" /> Monosodium Glutamate</div>
</div>



var viewModel = {
        wantsSpam: ko.observable(true),
        spamFlavor: ko.observable("almond") // Initially selects only the Almond radio button
    };

    // ... then later ...
    viewModel.spamFlavor("msg"); // Now only Monosodium Glutamate is checked​

这是链接http://jsfiddle.net/HhXGH/55/

1 个答案:

答案 0 :(得分:0)

您在底部缺少此行:

ko.applyBindings(视图模型);

仅供参考:如果您不知道ko.applyBindings是什么,以及它如何与Knockout绑定进行交互,请阅读此处的“激活淘汰赛”部分:http://knockoutjs.com/documentation/observables.html

编辑:更新小提琴

http://jsfiddle.net/jearles/HhXGH/56/