Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/424.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用Backbone.js捕获表单提交_Javascript_Backbone.js_Javascript Events - Fatal编程技术网

Javascript 如何使用Backbone.js捕获表单提交

Javascript 如何使用Backbone.js捕获表单提交,javascript,backbone.js,javascript-events,Javascript,Backbone.js,Javascript Events,当我在输入字段中输入数据后测试并单击我的apply按钮时,我得到一个文件未找到错误 Login按钮是一个虚拟按钮,没有任何功能。我只想在单击“应用”后显示一个警告框,上面写着“您以(此处的用户名)身份成功登录!!!” var Credentials = Backbone.Model.extend({}); var LoginView = Backbone.View.extend({ el: $("#login-form"), events: { "click #login":

当我在输入字段中输入数据后测试并单击我的
apply
按钮时,我得到一个文件未找到错误

Login
按钮是一个虚拟按钮,没有任何功能。我只想在单击“应用”后显示一个警告框,上面写着“您以(此处的用户名)身份成功登录!!!”

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()});
});


您的注册表格:
名字
姓
电话号码。
用户名
密码
申请
登录
为什么找不到文件错误?
由于表单已提交且操作为
“/login”
,默认方法为
get
请求,因此提交会向
login
页面发出
get
请求,但服务器上不存在该请求,因此会出现“未找到文件”错误。服务器返回一个
文件未找到
错误

如何防止提交? 您需要使用JavaScript停止提交。为此,首先捕获submit事件,然后对submit事件对象调用
.preventDefault()

如何用主干网捕获提交事件? 主干网提供基于视图的服务

事件散列(或方法)可用于指定一组DOM事件 这将通过绑定到视图上的方法

以下是捕获
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);
    }
});
当使用上面的代码时,为什么不起作用? 请注意,视图的根元素是用指定的

在初始代码中,您使用查找表单元素并将其传递给视图。但是为了让它工作,表单元素必须在运行视图的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>

//你的观点等等。
为什么找不到文件错误?
由于表单已提交且操作为
“/login”
,默认方法为
get
请求,因此提交会向
login
页面发出
get
请求,但服务器上不存在该请求,因此会出现“未找到文件”错误。服务器返回一个
文件未找到
错误

如何防止提交? 您需要使用JavaScript停止提交。为此,首先捕获submit事件,然后对submit事件对象调用
.preventDefault()

如何用主干网捕获提交事件? 主干网提供基于视图的服务

事件散列(或方法)可用于指定一组DOM事件 这将通过绑定到视图上的方法

以下是捕获
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);
    }
});
当使用上面的代码时,为什么不起作用? 请注意,视图的根元素是用指定的

在初始代码中,您使用查找表单元素并将其传递给视图。但是为了让它工作,表单元素必须在运行视图的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>

//你的观点等等。


寻求调试帮助的问题(“为什么此代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现这些问题所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参阅:如何创建。虽然您提供了代码,但它应该用最短的代码示例演示问题。此外,对于现场可运行的代码,如果不能,请使用代码示例(将代码缩进4个空格)。很好的提示和反馈Emile。好的,我看到了改进,但可能会更短。我们不需要CSS,因此可以完全删除它。我们不需要5个输入字段,事实上,根本不需要输入字段(以及管理其数据的主干代码)。所有的HTML样板也都有问题,我们只需要相关的HTML(
#登录表单
)来回答。寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现它所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参阅:如何创建。虽然您提供了代码,但它应该用最短的代码示例演示问题。此外,对于现场可运行的代码,如果不能,请使用代码示例(将代码缩进4个空格)。很好的提示和反馈Emile。好的,我看到了改进,但可能会更短。我们不需要CSS,因此可以完全删除它。我们不需要5个输入字段,事实上,根本不需要输入字段(以及管理其数据的主干代码)。所有HTML样板文件也都有问题,我们只需要相关的HTML(
#登录表单
)来回答。您会收到一个文件未找到错误,因为表单已提交,
操作为“/login”
,因此它尝试加载登录页面,但服务器上不存在该页面。好的,现在我理解了错误使用该操作的缺陷。@AndreasLambadarios因为我们正在捕获提交事件,表单的
操作
现在不相关了。它可以是任何东西,我们不用它。我刚刚解释了错误的来源。你解释的每一件事都非常清楚和简洁。当我测试你回答的代码时,出于某种原因,警告框没有出现在sumbit上。你重新做标记的方式让我更清楚。还有html页面结构windo