Knockout在Jquery对话框中单击绑定

时间:2013-07-19 20:23:31

标签: jquery knockout.js

我在页面上使用了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
});

然后在视图模型中我有

...

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

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

绑定已设置,但它们仅对此对话框中的元素不起作用。 如果我删除jquery对话框代码,它的工作原理。 任何想法我需要在这做什么?

2 个答案:

答案 0 :(得分:1)

试试这个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 :(得分:0)

你需要改变它等于函数

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

or 

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