淘汰MVC Html.RadioButton

时间:2013-08-14 06:06:18

标签: asp.net-mvc-3 knockout.js

我正在尝试使用此示例:

    <label><input type="checkbox" data-bind="checked: displayMessage" /> Display message</label>
    <div data-bind="if: displayMessage">Here is a message. Astonishing.</div>

<script type="text/javascript">
    ko.applyBindings({
        displayMessage: ko.observable(false)
    });
</script>

我的问题是如果选中以下RadioButton,如何显示消息。

<%= Html.RadioButton("answer", qm.DynamicQuestions[index].Answer_A, (qm.LearnerAnswers[index] == qm.DynamicQuestions[index].Answer_A))%><%= Html.Encode( qm.DynamicQuestions[index].Answer_A) %>

感谢您的建议。

1 个答案:

答案 0 :(得分:0)

我猜你想要这样的东西

http://jsfiddle.net/gonefishern/ktcY5/

<ul data-bind="foreach: questions">
    <li>
        <span data-bind="text: description"></span>
        <ul data-bind="foreach: availableAnswers">
            <li>
                <label>
                    <input type="radio" data-bind="value: value, checked: checked, attr: {name: $parent.id }" />
                    <span data-bind="text: value"></span>
                </label>
            </li>
        </ul>
    </li>
</ul>


var questions = [{
    id: 'ID1',
    description: ko.observable('Should I answer this?'),
    availableAnswers: [{
        value: 'Yes',
        checked: ko.observable('Yes')
    }, {
        value: 'No',
        checked: ko.observable(false)
    }]
},{
    id: 'ID2',
 description: ko.observable('How about answering this?'),
    availableAnswers: [{
        value: 'Yes',
        checked: ko.observable()
    }, {
        value: 'No',
        checked: ko.observable('No')
    }]
}];

var vm = {
    questions: questions,

}

ko.applyBindings(vm);