2014-05-17 141 views
3

我試圖從窗體中單擊提交按鈕時調用一個函數。Knockoutjs訂閱按鈕點擊?

<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函數的內部的功能。我可以用click:binding來調用它嗎?如果沒有,我可以以某種方式訂閱按鈕,如可觀察的?約

回答

2

這個怎麼樣:

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

+1

我不明白設置功能爲母體的性質功能。謝啦! – Jimbob