首页 文章

Knockout在Jquery对话框中单击绑定

提问于
浏览
0

我在页面上使用knockout.js视图模型,一切正常,除了我设置为jquery对话框的div中的单击绑定

这是div

<div id="CancelModal" title="Cancel">
    Changes to the definition have been detected. Do you want to exit and discard    the changes?"   
    <div style="position: absolute; bottom: 8px; right: 8px; text-align: right">
        <input type="button" value="Yes" data-bind="click: cancelConfirm" />
        <input type="button" value="No" data-bind="click: cancelDeny" />
    </div>
</div>

那我的jquery

$("#CancelModal").dialog({
    modal: true,
    autoOpen: false,
    width: 400,
    minHeight: 150,
    maxHeight: 150,
    position: "center",
    resizable: false
});

然后在我的viewmodel中

...

cancelConfirm() {
    alert("confirm");
}

cancelDeny() {
    alert("deny");
}

绑定已设置但同样它们不适用于此对话框中的元素 . 如果我删除jquery对话框代码,它的工作原理 . 我在这里需要做什么?

2 回答

  • 0

    试试这个http://jsfiddle.net/76EEt/1/

    HTML

    <a href="#" data-bind="click: $root.openDialog"> Open dialog </a> 
    <div id="CancelModal" title="Cancel">
        Changes to the definition have been detected. Do you want to exit and discard    the changes?"   
        <div style="position: absolute; bottom: 8px; right: 8px; text-align: right">
            <input type="button" value="Yes" data-bind="click: cancelConfirm" />
            <input type="button" value="No" data-bind="click: cancelDeny" />
        </div>
    </div>
    

    JS

    $("#CancelModal").dialog({
        modal: true,
        autoOpen: false,
        width: 400,
        minHeight: 150,
        maxHeight: 150,
        position: "center",
        resizable: false
    });
    
    var DataViewModel = function() {
        var self = this;
    
        self.cancelConfirm = function () {
             alert("confirm");
        };
    
        self.cancelDeny = function () {
             alert("deny");
        };
    
        self.openDialog = function () {
            $("#CancelModal").dialog("open");
        };
    };
    
    ko.applyBindings(new DataViewModel());
    
  • 1

    你需要改变它等于一个函数

    self.cancelConfirm = function() {
        alert("confirm");
    }
    
    or 
    
    this.cancelConfirm = function() {
        alert("confirm");
    }
    

相关问题