2016-03-16 38 views
0

我很新的KnockoutJS,並已注意到有不少在那裏大約單選按鈕和淘汰賽,但遺憾的是沒有這些能幫助我。不能改變單選按鈕的選擇 - KnockoutJS

我有兩個不同的容器中兩個單選按鈕,可以選擇其中之一,但選擇另外一個時,遺憾的是不改變選擇,即使值等於傳遞給檢查綁定的值。下面是簡單的HTML:

<div> 
    <label> 
    <input 
      type="radio" 
      name="type-1" 
      data-bind="click: function(data, event) { setUserClass('User_Classification_1', data, event); }, checked: userClass" 
      value="User_Classification_1"/> 
    Some Text 
    </label> 
</div> 
<div> 
    <label> 
    <input 
      type="radio" 
      name="type-1" 
      data-bind="click: function(data, event) { setUserClass('User_Classification_2', data, event);}, checked: userClass" 
      value="User_Classification_2"/> 
    Some Text. 
    </label> 
</div> 

而且這裏的JS:

var certViewModel = function() { 

     var self = this; 

     self.userClass = ko.observable(undefined); 

     self.setUserClass = function(uClass, data, event) { 
      self.userClass(uClass); 
      console.log(self.userClass()); 
     }; 
    }; 

    var viewModel = new certViewModel(); 
ko.applyBindings(viewModel); 

這裏是一個小提琴,顯示問題的例子: https://jsfiddle.net/1vu0skpg/

編輯:修正了它自己。從點擊處理程序返回正確的問題。

回答

0

就在我的兩分錢扔...

HTML

<div> 
    <label> 
    <input 
      type="radio" 
      name="type-1" 
      data-bind="checked: userClass" 
      value="User_Classification_1"/> 
    Some Text 
    </label> 
</div> 
<div> 
    <label> 
    <input 
      type="radio" 
      name="type-1" 
      data-bind="checked: userClass" 
      value="User_Classification_3"/> 
    Some Text. 
    </label> 
</div> 

This is the selected radio: <strong><span data-bind="text: userClass"></strong> 

腳本

var certViewModel = function() { 
    var self = this; 

    self.userClass = ko.observable(); 
}; 

var viewModel = new certViewModel();  
ko.applyBindings(viewModel); 

我認爲你得太多辦法這一點,除非你是triyng做一些真正具體?

https://jsfiddle.net/1vu0skpg/5/