我在点击表单中的提交按钮时尝试调用函数。
<div id="myBrowser" data-bind="with: QuestionFilter">
<form name="Homeworks" data-bind="submit: searchMeNow">
<button data-bind="click: $root.submit, clickBubble: false">Search</button>
searchMeNow
是QuestionFilter
函数内的函数。我可以通过点击来调用它吗:绑定?如果没有,我可以以某种方式订阅按钮吗?
答案 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