Simple button click in backbone

不想你离开。 提交于 2019-12-24 11:44:43

问题


I am trying out different functionalities using backbone and i came accross a strange one. I am trying to submit a form through backbone. I had done this previously and i cannot find whats wrong with what i am doing.

The code is as follows :

HTML Part

   <div clas="loginpage"></div>
   <form class="login-user-form">
     <input type="text" name="name" id="name" placeholder="Enter Name"><br><br>
     <button type="submit" class="btn">Create</button>
   </form>

jQuery Part

var UserLogin = Backbone.View.extend({
   el:'.loginpage',
   initialize:function(){
   console.log("Login View Initialized");
 },
 events:{
   'submit .btn' : 'loginuser'
 },
 loginuser:function(){
   console.log("Login Clicked.");
   return false;
 }
});
var userlogin = new UserLogin();

I get Login View Initialized message in console. But i cannot get the loginuser function to work. The page submits through its default submit functionality.

What am i doing wrong?


回答1:


1) loginpage doesn't contain the form. Fix:

<div class="loginpage">
  <form class="login-user-form">
    <input type="text" name="name" id="name" placeholder="Enter Name"><br><br>
    <button type="submit" class="btn">Create</button>
  </form>
</div>

2)

events : {
 'submit' : 'loginuser'
},

loginuser : function(){
  console.log("Login Clicked.");
  return false; // Stops default html form submission
}



回答2:


Got it working :

events:{   
    'submit' : 'loginuser'
}

Got this from the following thread : How do I get backbone to bind the submit event to a form?

Cheers ..:)



来源:https://stackoverflow.com/questions/17466801/simple-button-click-in-backbone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!