knockout js radio button click event reset selection

后端 未结 3 1080
无人共我
无人共我 2021-02-13 02:46

I have bind \"checked\" and \"click\" event on a radio button list. But whenever a radio button is clicked, the selection does not stay. I must be doing something really wrong.

3条回答
  •  灰色年华
    2021-02-13 03:13

    You just remove the click event or use return true from click event. Because Knockout prevent the click event from taking any default action. This means that if you use the click binding on an a tag (a link), for example, the browser will only call your handler function and will not navigate to the link’s href

    var viewModel = {
        wantsSpam: ko.observable(true),
        spamFlavor: ko.observable('cherry'),
        /*click: function(){
          alert('Hi');
        }*/
    };
    

    Or

    var viewModel = {
        wantsSpam: ko.observable(true),
        spamFlavor: ko.observable('cherry'),
        click: function(){
          alert('Hi');
          return true;
        }
    };
    

提交回复
热议问题