淘汰赛js:单选按钮切换

时间:2014-04-24 23:04:50

标签: javascript knockout.js

为什么我的单选按钮不起作用?

notes: ko.computed(function(){
   if (!reservation_ui) {
          global_obsv();
          return [];
    }
                var notes = [];
                var my_notes = reservation_ui.globals().notes;
                for(var i=0; i < my_notes.length; i++){
                    if(my_notes[i].attributes.length){
                        my_notes[i]['ischecked'] = ko.observable(false);
                        for(var j = 0; j < my_notes[i].attributes.length; j++){
                            my_notes[i].attributes[j]['ischecked'] = ko.computed({
                                read: (function(note, attr){
                                    return function(){
                                        return note.ischecked() == 'test-'+attr.id;
                                    };
                                })(my_notes[i], my_notes[i].attributes[j]),
                                write: (function(note, attr){
                                    return function(value){
                                        note.ischecked('test-'+attr.id);
                                    };
                                })(my_notes[i], my_notes[i].attributes[j]),
                            });
                        }
                        notes.push(my_notes[i]);
                    }
                }

                return notes;
            })

<!-- ko foreach: notes -->
                                <div class="package-wrapper">
                                    <h1 class="package-wrapper" data-bind="text: name"></h1>
                                    <!-- ko foreach: attributes -->
                                        <div>
                                            <div><input data-bind="checked:$parent.ischecked() == 'test-'+id, click:ischecked, attr: {value: 'test-'+id, type: $parent.multi ? 'checkbox' : 'radio', name: 'note-'+$parent.id, id: 'note-'+id }" > <label data-bind="attr: {for: 'note-'+id}, text: name"></label></div>
                                            <div data-bind="visible:$parent.ischecked() == 'test-'+id, text: $parent.ischecked">
                                                howdy
                                            </div>
                                        </div>
                                    <!-- /ko -->
                                </div>
                            <!-- /ko -->

我真正想做的是让我的单选按钮切换,但我无法让他们开始工作:)

1 个答案:

答案 0 :(得分:0)

您不能只绑定click: ischecked,因为这不会切换ischecked

尝试click: function() { ischecked(!ischecked()) }

(如果有效,你应该重构而不是内联。)