Simple button click in backbone

2019-08-06 13: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?

2条回答
干净又极端
2楼-- · 2019-08-06 14:33

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
}
查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-08-06 14:39

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 ..:)

查看更多
登录 后发表回答