【问题标题】:Knockout check binding doesn`t change model value by jquery "change" event剔除检查绑定不会通过 jquery“更改”事件更改模型值
【发布时间】:2013-04-04 00:51:22
【问题描述】:

我找到了这个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"/>

小提琴:

http://jsfiddle.net/skiff/4Wnmu/3/

【问题讨论】:

  • 你能详细说明I would like to have ability to change model value without trigger "click" event.吗?
  • 我有一个 js ui 框架,它处理点击并触发 Change 事件。我不能改变它。所以我需要有能力查看 viewModel 中的用户变化

标签: jquery knockout.js


【解决方案1】:

在与单选按钮的剔除检查绑定中,该属性应等于单选按钮的值。并且值始终是字符串。检查这个工作小提琴:

Working Fiddle.

JS

var vm = new AppViewModel();

$("#change").click(function () {
    vm.TimeType("2");
    alert(vm.TimeType());
});   

function AppViewModel() {
    this.TimeType = ko.observable("1");
}

ko.applyBindings(vm);

【讨论】:

  • 它应该是所有视图模型的解决方案。所以代码中的某处触发了“更改”。而且我无法将 viewModel 添加到此代码中。我只能看到,触发了什么变化。
【解决方案2】:

That 是您要找的吗?

viewModel.TimeType.subscribe();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 2017-12-08
    • 2016-10-20
    • 1970-01-01
    • 1970-01-01
    • 2018-08-21
    • 2020-07-10
    相关资源
    最近更新 更多