我使用此代码显示两个单选按钮,供用户选择Yes或No。
<fieldset data-role="controlgroup">
<legend>Choose</legend>
<input type="radio" name="pref" id="radio1" value="yes" data-bind="checked: $root.selected" />
<label for="radio1">YES</label>
<input type="radio" name="pref" id="radio2" value="no" data-bind="checked: $root.selected" />
<label for="radio2">NO</label>
</fieldset>显示页面时,无论$root.selected的值(是/否)如何,都不会选中这两个单选按钮。经过多次调试,我发现如果我删除label元素,或者即使我将label标签更改为span/div标签,它也可以工作。
你知道为什么标签上没有选中单选按钮吗?
谢谢。
发布于 2014-08-02 20:30:10
jQM隐藏了原生电台/复选框,并将它们包装在label中。label接收所有样式,使其看起来是选中的。当您将单选/复选框的.prop()更改为true或false时,需要手动重新增强/刷新元素$(element).checkboxradio("refresh")。
我不是knockout.js专家,但是下面是一个实现你想要的东西的例子。
var viewModel = function (status, elm) {
var self = this;
self.bar = ko.observable(status ? status.bar : false);
/* refresh radio/checkbox to re-apply styles */
self.foo = $(elm).prop("checked", self.bar()).checkboxradio("refresh");
};
ko.applyBindings(new viewModel({
bar: true
}, $("#radio1")));https://stackoverflow.com/questions/25090134
复制相似问题