Knockoutjs订阅按钮点击?

时间:2014-05-17 19:05:15

标签: javascript knockout.js

我在点击表单中的提交按钮时尝试调用函数。

<div id="myBrowser" data-bind="with: QuestionFilter">
<form name="Homeworks" data-bind="submit: searchMeNow">
<button data-bind="click: $root.submit, clickBubble: false">Search</button>

searchMeNowQuestionFilter函数内的函数。我可以通过点击来调用它吗:绑定?如果没有,我可以以某种方式订阅按钮吗?

1 个答案:

答案 0 :(得分:1)

这个怎么样:

HTML:

<div id="myBrowser" data-bind="with: QuestionFilter">
<form name="Homeworks" data-bind="submit: searchMeNow">
<button data-bind="click: $root.submit, clickBubble: false">Search</button>

JavaScript的:

var ViewModel = function(){

    function QuestionFilter(){
        var self = this;
        self.searchMeNow = function(){
        };
    }

    return{
        submit: function(){
            alert("Hello world");
        },
        QuestionFilter:function(){
            return new QuestionFilter();
        }
    }; 
};
ko.applyBindings(new ViewModel());

这是完整的version