我发现这个enter link description here anwser,但对我来说没什么好处。
我希望能够在不触发“点击”事件的情况下更改模型值。所以现在当用户改变价值(而不是小提琴)时,一些代码会产生“改变”事件。但模型并没有改变它的价值。
所以我有一个
模型
function AppViewModel() {
//Common
this.TimeType = ko.observable(0);
}
$(document).ready(function () {
viewModel = new AppViewModel();
ko.applyBindings(viewModel, $("#testId")[0]);
$("#change").click(function () {
$("#third").attr('checked', 'checked');
$("#third").change();
alert(viewModel.TimeType());
});
});
HTML
<div id="testId" class="holder main-holder">
<div class="holder">
<input checked="checked" data-bind="checked: TimeType" id="Time" name="Type" type="radio" value="0"><label for="Time" class=" ">First</label>
</div>
<div class="holder">
</div><input data-bind="checked: TimeType" id="TimeWithTypeDay" name="Type" type="radio" value="1"><label for="TimeWithTypeDay">Second</label>
</div>
<div class="holder">
<input id="third" data-bind="checked: TimeType" id="TimeWithDaysOfWeek" name="Type" type="radio" value="2"><label for="TimeWithDaysOfWeek" >Third</label>
</div>
</div>
<input id="change" type="button" value ="Change"/>
小提琴:
答案 0 :(得分:1)
在带有单选按钮的敲除检查绑定中,属性应该等于单选按钮的值。而值总是字符串。检查这个工作小提琴:
JS
var vm = new AppViewModel();
$("#change").click(function () {
vm.TimeType("2");
alert(vm.TimeType());
});
function AppViewModel() {
this.TimeType = ko.observable("1");
}
ko.applyBindings(vm);
答案 1 :(得分:0)
That正是您要找的?
viewModel.TimeType.subscribe();