HTML Knockout单选按钮来调用函数

时间:2015-02-13 10:07:16

标签: javascript jquery html knockout.js radio-button

我使用knockout循环数组以显示带标签的单选按钮列表,我希望它能够在选择单选按钮时调用函数onInputChange

我目前的代码是:

<p data-bind="foreach:properties">
    <br>
    <input name="filter" type="radio" data-bind="checked:$root.sortType, event:$root.onInputChange">
    <label for="filter" data-bind="text:id"></label>
    <br>
</p>

2 个答案:

答案 0 :(得分:2)

你应该在下面

<input name="filter" type="radio" data-bind="checked:$root.sortType, event: { change: $root.onInputChange}">

答案 1 :(得分:2)

这样的东西?

&#13;
&#13;
var model = function() {
  var sortType = ko.observable("Two");
  var properties = ko.observableArray([{id: "One"}, {id: "Two"}, {id: "Three"}]);

  function onInputChange(item) {
    alert("changed to: " + item.id);
  }

  return {
    sortType: sortType,
    properties: properties,
    onInputChange: onInputChange
  };
}

ko.applyBindings(model);
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>

<p data-bind="foreach:properties">
  <br>
  <input name="filter" type="radio"
      data-bind="value: id, 
         checked:$parent.sortType, 
         event: {change : $parent.onInputChange }">
  <label for="filter" data-bind="text:id"></label>
  <br>
</p>
&#13;
&#13;
&#13;