如何使用 Backbone.js 捕获表单提交
How to catch form submit with Backbone.js
当我在输入字段中输入数据后测试并单击 apply
按钮时,我收到 找不到文件错误。
Login
按钮是一个没有任何功能的虚拟按钮。我只想在单击应用后显示一个警告框,上面写着 "You logged in as (user name here) Succesfully!!!"。
var Credentials = Backbone.Model.extend({});
var LoginView = Backbone.View.extend({
el: $("#login-form"),
events: {
"click #login": "login"
},
initialize: function(){
var self = this;
this.firstname = $("#username");
this.lastname = $("#lastname");
this.number = $("#number");
this.username = $("#username");
this.password = $("#password");
this.firstname.change(function(e){
self.model.set({firstname: $(e.currentTarget).val()});
});
this.lastname.change(function(e){
self.model.set({lastname: $(e.currentTarget).val()});
});
this.number.change(function(e){
self.model.set({number: $(e.currentTarget).val()});
});
this.username.change(function(e){
self.model.set({username: $(e.currentTarget).val()});
});
this.password.change(function(e){
self.model.set({password: $(e.currentTarget).val()});
});
},
login: function(){
var firstn= this.model.get('firstname');
var lastn= this.model.get('lastname');
var numb= this.model.get('number');
var user= this.model.get('username');
var pword = this.model.get('password');
alert("You logged in as " + user + "Succesfully!!!");
return false;
}
});
window.LoginView = new LoginView({model: new Credentials()});
});
<form action="/login" id="login-form" align="left">
<h1> Your Registration Form:</h1>
First Name <input type="text" id="firstname" placeholder="First Name">
Last Name <input type="text" id="lastname" placeholder="Last Name">
Phone No. <input type="text" id="number" placeholder="1(555)555-5555">
UserName <input type="text" id="username" placeholder="UserName">
Password <input type="password" id="password" placeholder="Password">
<button id="login" onclick="">Apply</button>
<!-- dummy button -->
<button id="login-button">Login</button>
</form>
为什么 找不到文件 错误?
您收到找不到文件的错误,因为表单已提交且操作是 "/login"
,默认方法是 GET
请求,因此提交会发出 GET
请求转到 login
页面,但它不存在于服务器上。服务器 returns 一个 File not found
错误。
如何防止提交?
您需要使用 JavaScript 停止提交。为此,首先捕获提交事件,然后在提交事件对象上调用 .preventDefault()
。
如何使用 Backbone 捕获提交事件?
Backbone 提供 events
property 的观看次数。
The events hash (or method) can be used to specify a set of DOM events
that will be bound to methods on your View through delegateEvents
.
以下是捕获 submit
事件的最简单方法,假设视图的根元素是表单,就像在您的代码中一样。
events: {
"submit": "onSubmit",
},
onSubmit: function(e) {
// `e` being a standard DOM event
e.preventDefault();
}
这里我简化了你的观点:
var LoginView = Backbone.View.extend({
// Put the string into a template to ease the manipulation later on.
template: _.template("You logged in as <%= username %> and a password of <%= password %>\nFirstName:<%= firstname %>\nLastName:<%= lastname %>\nNumber:<%= number %>"),
el: $("#login-form"),
events: {
// listen for the submit event of the form
"submit": "onSubmit",
// listen to events from here
"change #username": 'onUsernameChange'
},
initialize: function() {
// it's a good idea to cache jQuery objects like this.
this.firstname = $("#username");
this.lastname = $("#lastname");
this.number = $("#number");
this.username = $("#username");
this.password = $("#password");
// but avoid extensive `change` listeners as it's inefficient and
// useless in this case. If you want to listen to changes, do it
// in the events hash, like the "onUsernameChange" example.
},
onSubmit: function(e) {
// prevent the submit and do what you want instead
e.preventDefault();
// Set directly with an object, it's quick and clean.
this.model.set({
firstname: this.firstname.val(),
lastname: this.lastname.val(),
number: this.number.val(),
username: this.username.val(),
password: this.password.val()
});
// use the template for the alert.
alert(this.template(this.model.toJSON()));
},
onUsernameChange: function(e) {
// no need for jQuery for a trivial value retrieval
console.log(e.currentTarget.value);
}
});
指定表单按钮类型属性,因为它默认为 submit
。因此,将 #login-button
设为 type="button"
可确保它不会触发提交。
<button type="submit" id="login">Apply</button>
<!-- dummy button -->
<button type="button" id="login-button">Login</button>
为什么使用上面的确切代码时它不起作用?
请注意,视图的根元素是用 el
property 指定的。
在您的初始代码中,您使用 jQuery's core function 查找表单元素并将其传递给视图。但要使其工作,表单元素必须存在于 运行 视图的 JS 之前。
因此 HTML 页面结构应该如下所示:
<html>
<head>
<!-- head stuff like CSS, title, etc. -->
</head>
<body>
<form id="login-form">
<!-- rest of the form goes here -->
</form>
<!-- Load the scripts here -->
<script src="libs/jquery/dist/jquery.js"></script>
<script src="libs/underscore/underscore.js"></script>
<script src="libs/backbone/backbone.js"></script>
<!-- then your own code can go here, or into another js file. -->
<script>
// your view, etc.
</script>
</body>
</html>
当我在输入字段中输入数据后测试并单击 apply
按钮时,我收到 找不到文件错误。
Login
按钮是一个没有任何功能的虚拟按钮。我只想在单击应用后显示一个警告框,上面写着 "You logged in as (user name here) Succesfully!!!"。
var Credentials = Backbone.Model.extend({});
var LoginView = Backbone.View.extend({
el: $("#login-form"),
events: {
"click #login": "login"
},
initialize: function(){
var self = this;
this.firstname = $("#username");
this.lastname = $("#lastname");
this.number = $("#number");
this.username = $("#username");
this.password = $("#password");
this.firstname.change(function(e){
self.model.set({firstname: $(e.currentTarget).val()});
});
this.lastname.change(function(e){
self.model.set({lastname: $(e.currentTarget).val()});
});
this.number.change(function(e){
self.model.set({number: $(e.currentTarget).val()});
});
this.username.change(function(e){
self.model.set({username: $(e.currentTarget).val()});
});
this.password.change(function(e){
self.model.set({password: $(e.currentTarget).val()});
});
},
login: function(){
var firstn= this.model.get('firstname');
var lastn= this.model.get('lastname');
var numb= this.model.get('number');
var user= this.model.get('username');
var pword = this.model.get('password');
alert("You logged in as " + user + "Succesfully!!!");
return false;
}
});
window.LoginView = new LoginView({model: new Credentials()});
});
<form action="/login" id="login-form" align="left">
<h1> Your Registration Form:</h1>
First Name <input type="text" id="firstname" placeholder="First Name">
Last Name <input type="text" id="lastname" placeholder="Last Name">
Phone No. <input type="text" id="number" placeholder="1(555)555-5555">
UserName <input type="text" id="username" placeholder="UserName">
Password <input type="password" id="password" placeholder="Password">
<button id="login" onclick="">Apply</button>
<!-- dummy button -->
<button id="login-button">Login</button>
</form>
为什么 找不到文件 错误?
您收到找不到文件的错误,因为表单已提交且操作是 "/login"
,默认方法是 GET
请求,因此提交会发出 GET
请求转到 login
页面,但它不存在于服务器上。服务器 returns 一个 File not found
错误。
如何防止提交?
您需要使用 JavaScript 停止提交。为此,首先捕获提交事件,然后在提交事件对象上调用 .preventDefault()
。
如何使用 Backbone 捕获提交事件?
Backbone 提供 events
property 的观看次数。
The events hash (or method) can be used to specify a set of DOM events that will be bound to methods on your View through
delegateEvents
.
以下是捕获 submit
事件的最简单方法,假设视图的根元素是表单,就像在您的代码中一样。
events: {
"submit": "onSubmit",
},
onSubmit: function(e) {
// `e` being a standard DOM event
e.preventDefault();
}
这里我简化了你的观点:
var LoginView = Backbone.View.extend({
// Put the string into a template to ease the manipulation later on.
template: _.template("You logged in as <%= username %> and a password of <%= password %>\nFirstName:<%= firstname %>\nLastName:<%= lastname %>\nNumber:<%= number %>"),
el: $("#login-form"),
events: {
// listen for the submit event of the form
"submit": "onSubmit",
// listen to events from here
"change #username": 'onUsernameChange'
},
initialize: function() {
// it's a good idea to cache jQuery objects like this.
this.firstname = $("#username");
this.lastname = $("#lastname");
this.number = $("#number");
this.username = $("#username");
this.password = $("#password");
// but avoid extensive `change` listeners as it's inefficient and
// useless in this case. If you want to listen to changes, do it
// in the events hash, like the "onUsernameChange" example.
},
onSubmit: function(e) {
// prevent the submit and do what you want instead
e.preventDefault();
// Set directly with an object, it's quick and clean.
this.model.set({
firstname: this.firstname.val(),
lastname: this.lastname.val(),
number: this.number.val(),
username: this.username.val(),
password: this.password.val()
});
// use the template for the alert.
alert(this.template(this.model.toJSON()));
},
onUsernameChange: function(e) {
// no need for jQuery for a trivial value retrieval
console.log(e.currentTarget.value);
}
});
指定表单按钮类型属性,因为它默认为 submit
。因此,将 #login-button
设为 type="button"
可确保它不会触发提交。
<button type="submit" id="login">Apply</button>
<!-- dummy button -->
<button type="button" id="login-button">Login</button>
为什么使用上面的确切代码时它不起作用?
请注意,视图的根元素是用 el
property 指定的。
在您的初始代码中,您使用 jQuery's core function 查找表单元素并将其传递给视图。但要使其工作,表单元素必须存在于 运行 视图的 JS 之前。
因此 HTML 页面结构应该如下所示:
<html>
<head>
<!-- head stuff like CSS, title, etc. -->
</head>
<body>
<form id="login-form">
<!-- rest of the form goes here -->
</form>
<!-- Load the scripts here -->
<script src="libs/jquery/dist/jquery.js"></script>
<script src="libs/underscore/underscore.js"></script>
<script src="libs/backbone/backbone.js"></script>
<!-- then your own code can go here, or into another js file. -->
<script>
// your view, etc.
</script>
</body>
</html>