【发布时间】:2018-04-13 23:36:08
【问题描述】:
我试图从单选按钮列表中获取选中的值,为此我试图将选中的值加载到 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">
如果单选按钮被选中,为什么 'checked' 事件不会触发所需的操作?
<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" });
}
【问题讨论】:
-
输入元素末尾的“必需”文本是什么?
-
@Arnab 必须检查一个选项才能提交表单