用户点击KnockoutJS进入时的调用功能

时间:2014-05-29 12:50:39

标签: knockout.js knockout-mapping-plugin knockout-mvc

我有下面的注册表格代码,当用户输入他们的数据并点击输入时,RegisterFunction应该触发但在下面的情况下它不会发生而不是手动点击锚标记。有什么想法吗?

    <form id="Register">
     <a class="btn green pull-right" data-bind="click: RegisterFunction"> Login</a>
    </form>

RegisterFunction : function () {
//logic goes here 
}

谢谢

2 个答案:

答案 0 :(得分:2)

<form id="Register">
     <a class="btn green pull-right" data-bind="event: { keypress: RegisterFunction }"> Login</a>
    </form>

RegisterFunction : function (data, event) {
              var evt = event;
               if (window.event != undefined) { evt = window.event };
              var keycode = (event.which) ? event.which : evt.keyCode;
    if (keycode == 13) { //13 is key cod for enter key
           //call your function here  
           MyFunctionCall();
     }

}

答案 1 :(得分:0)

Knockout也提供了submit binding - read more about submit binding

<form id="Register" data-bind="submit: RegisterFunction">
    <a class="btn green pull-right" data-bind="click: RegisterFunction"> Login</a>
</form>

RegisterFunction : function () {
//logic goes here 
}