问题
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