标签内的已检查无线电不会触发事件

时间:2018-04-13 08:15:42

标签: knockout.js knockout-3.0

我试图从单选按钮列表中获取已检查的值,为此我尝试将已检查的值加载到一个observable中。从文档中可以看出这是建议的方式:

<input type="radio" name="type"  data-bind="value:ct.value, checked: $root.selectedtype" required>

然而,这在我的案例中并不起作用。我的收音机周围有一个标签,如果我在标签上添加了点击事件,它会正确填充可观察的selectedtype ,这样我就可以获得所选的值,例如:

 <label class="btn btn-outline-dark" data-bind="click:$root.selectedtype">

如果单选按钮被选中,为什么选中&#39;&#39;事件不会触发所需的操作?

 <label for="email">Company Type</label>
                    <div data-toggle="buttons" class="form-group label-block">
                        <div class="row">
                            <div class="col-sm-6" data-bind="foreach: { data: companytype, as: 'ct' }">
//-->   this way it works       <label class="btn btn-outline-dark" data-bind="click:$root.selectedtype"> 
                                    <input type="radio" name="type"  data-bind="value:ct.value" required>
//-->   this way it does not work          <input type="radio" name="type"  data-bind="value:ct.value, checked: $root.selectedtype" required>
                                    <span data-bind="text: ct.label"></span>
                                </label>
                            </div>
                        </div>
                        <div class="help-block with-errors"></div>
                    </div>

 Selected: <span data-bind="text: selectedtype().value"></span>-

在我的模特中:

   var companytype = ko.observableArray([]);
   var selectedtype = ko.observable('');

   var init = function () {
        getItemList();
        companytype.push({ "label": "LTD", "value": "1" });
        companytype.push({ "label": "PLC", "value": "2" });        
        companytype.push({ "label": "LLP", "value": "3" });
        companytype.push({ "label": "Sole Trader", "value": "4" });
    }

2 个答案:

答案 0 :(得分:1)

var companytype = ko.observableArray([]);
var selectedtype = ko.observable('');

var init = function() {
  //getItemList();
  companytype([
    { "label": "LTD", "value": "1" },
    { "label": "PLC", "value": "2" },
    { "label": "LLP", "value": "3" },
    { "label": "Sole Trader", "value": "4" }
  ]);
}

init();
var viewModel = { companytype, selectedtype };

ko.applyBindings(viewModel);
.btn {
  border: 1px solid black;
  padding: 2px 4px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<label for="email">Company Type</label>
<div data-toggle="buttons" class="form-group label-block">
  <div class="row">
    <div class="col-sm-6" data-bind="foreach: { data: companytype, as: 'ct' }">
      <label class="btn btn-outline-dark" data-bind="click: function(){	$root.selectedtype($data); return true; }"> 
          <input type="radio" name="type"  data-bind="checked: $root.selectedType" required>
          <span data-bind="text: ct.label"></span>
      </label>
    </div>
  </div>
  <div class="help-block with-errors"></div>
</div>

Selected: <span data-bind="text: ko.toJSON(selectedtype)"></span>

您可以使用选中的绑定,但由于标签和单选按钮控件都试图处理点击事件,因此它们互相争斗。您需要让标签的点击功能返回“true”,以便事件不会继续到无线电控制。

<label class="btn btn-outline-dark" data-bind="click: function(){ $root.selectedtype($data); return true; }"> 
    <input type="radio" name="type" data-bind="checked: $root.selectedType" required>
    <span data-bind="text: ct.label"></span>
</label>

答案 1 :(得分:0)

这是你想要的吗?两个单选按钮都在工作:

var VM = function(){
    var self=this;
    self.companytype = ko.observableArray([]);
    self.selectedtype = ko.observable("");

    self.init = function () {
            //getItemList();
            self.companytype.push({ "label": "LTD", "value": "1" });
            self.companytype.push({ "label": "PLC", "value": "2" });        
            self.companytype.push({ "label": "LLP", "value": "3" });
            self.companytype.push({ "label": "Sole Trader", "value": "4" });
    }
}
var vm = new VM();
vm.init();
ko.applyBindings(vm);
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<label for="email">Company Type</label>
      <div data-toggle="buttons" class="form-group label-block">
        <div class="row">
         <div class="col-sm-6" data-bind="foreach: { data: companytype, as: 'ct' }">
            <input type="radio" name="type"  data-bind="value:ct.value, checked: $root.selectedtype" required> 
            <span data-bind="text: ct.label"></span>         
         </div>
        </div>
       <div class="help-block with-errors"></div>
      </div>

 Selected: <span data-bind="text: selectedtype()"></span>

相关问题