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
});

然后在我的视图模型中

...

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

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

绑定已设置,但同样它们仅不适用于此对话框中的元素。如果我删除 jquery 对话框代码,它就可以工作。任何想法我需要在这里做什么?

4

2 回答 2

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());
于 2013-07-19T20:50:40.127 回答
0

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

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

or 

this.cancelConfirm = function() {
    alert("confirm");
}
于 2013-07-19T20:50:55.740 回答