我要求允许用户取消选中单选按钮。
我创建了一个清除(取消选中)单选按钮的通用功能。这样做之后,我使用 jQuery 触发了 change 事件,但是 Observable 模型并没有更新它的值。
var UnCheck = "UnCheck";
$("input:radio.AllowUnCheck").click(function(event){
var $clickedbox = $(this),
radioname = $clickedbox.prop("name"),
$group = $('input[name|="'+ radioname + '"]'),
doUncheck = $clickedbox.hasClass(UnCheck),
isChecked = $clickedbox.is(':checked');
if(doUncheck){
$group.removeClass(UnCheck);
$clickedbox.prop('checked', false);
//Edit: Added this code as a work around.
if(kendo)if(this.kendoBindingTarget){
bindingSource = this.kendoBindingTarget.source;
if(bindingSource){
try{
// This assumes that the name of the radio group is the same as the
// as the observables key (set and get key).
bindingSource.set(radioname, "None");
}
catch(e){/*Do nothing*/}
}
};
}
else if(isChecked){
// Make sure that others in the group do not have the UnCheck class
// This will ensure that only one radio in the group will have the UnCheck class at a time.
$group.removeClass(UnCheck);
// Adding the class tells the function to uncheck it the next time this radio
// is clicked, if clicked before any other radio in the group is clicked.
$clickedbox.addClass(UnCheck);
}
//$clickedbox.trigger("change");
document.getElementById(this.id).onchange();
});
我也试过快捷$clickedbox.change()
方式document.getElementById("id").onchange();
那么,当我使用 JavaScript 更改值时,如何让 Observable 使用 UI 更新它的值?
请记住,进行更改的代码不知道元素绑定到 kendo Observable,并且不能依赖于 kendo-ui API。
编辑:我找不到解决办法,所以我添加了代码来检查剑道可观察绑定。按照使用无线电组名称作为访问相应 observable 的键的约定,我能够让模型适当地更新。