knockout js单选按钮单击事件重置选择 [英] knockout js radio button click event reset selection
问题描述
我在单选按钮列表上绑定了已检查和单击事件。但是,只要单击单选按钮,选择就不会停留。我一定是做错了。如果你们能指出我正确的方向,真的很感激。
I have bind "checked" and "click" event on a radio button list. But whenever a radio button is clicked, the selection does not stay. I must be doing something really wrong. Really appreciate if you guys can point me to the right direction.
请参阅小提琴
查看型号:
var viewModel = {
wantsSpam: ko.observable(true),
spamFlavor: ko.observable('cherry'),
click: function(model){
console.log(model);
}
};
查看:
<input type="radio" name="flavorGroup" value="cherry"
data-bind="checked: spamFlavor, click:click" />
推荐答案
来自 点击
事件文档:
From the click
event documentation:
默认情况下,Knockout会阻止
点击
事件采取任何默认操作。
...
但是,如果你想让默认的点击
操作继续,只需从你的点击处理程序返回true
函数。
By default, Knockout will prevent the
click
event from taking any default action.
...
However, if you do want to let the defaultclick
action proceed, justreturn true
from your click handler function.
因为你的点击
处理程序而你的单选按钮被重置了修复它你只需最后返回true
:
So your radio button is reset because of your click
handler and to fix it you just need to return true
at the end:
click: function(){
alert('Hi');
return true;
}
演示 JSFiddle 。
这篇关于knockout js单选按钮单击事件重置选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!