绑定处理程序和 HMTL。
ko.bindingHandlers.radioGroupChanged = {
init: function (element, valueAccessor, allBindingAccessor, viewModel, bindingContext) {
var value = valueAccessor();
var newValueAccessor = function () {
return {
change: function () {
var selectedValue = $(element).attr('selected');
value(selectedValue);
}
}
};
ko.bindingHandlers.event.init(element, newValueAccessor, allBindingAccessor, viewModel, bindingContext);
},
update: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
$(element).attr('selected', valueAccessor()());
}
}
<paper-radio-group style="position:absolute; left:160px; top:0px;" data-bind="radioGroupChanged: MessagePriority" }">
<paper-radio-button label="High">
</paper-radio-button>
<paper-radio-button label="Medium">
</paper-radio-button>
<paper-radio-button label="Low">
</paper-radio-button>
</paper-radio-group>
当它点击更新功能时,如果我手动更新单选组元素的选定属性,它会立即更改为“选择”。
$(element).attr('selected', '0');
那么 $(element).attr('selected') 总是'selected'。
我不知道为什么它没有保留价值。