Backbone.Syphon - 总是返回空对象

时间:2014-06-07 18:22:46

标签: jquery forms backbone.js

我在Backbone.Marionette应用中使用Backbone.Syphon插件。当我点击表单的提交按钮时,我总是得到空对象({})..我不知道原因,

任何人帮我找到问题pelase?

这是我的view.js

define([
    'jQuery','underscore',
    'backbone','marionette',
    'text!./templates/loginView.html'],
    function($,_,Backbone,Marionette,template){
        "use strict";

        var LoginView = Backbone.Marionette.ItemView.extend({

            className:'col-xs-12 col-md-4 col-md-offset-4',

            template:_.template(template),

            events:{
                "submit form" : "loginSubmit"
            },

            loginSubmit:function(e){
                e.preventDefault();
                var data = Backbone.Syphon.serialize(e.target);
                console.log(data); //always return as empty object.
            }

        });

        return LoginView;
    }
);

这是我的表格:

<form action="#" id="loginForm" class="form-horizontal" role="form">
    <legend>
        <fieldset>
            <label for="userName">UserName</label>
            <input type="text" value="" id="userName" class="form-control" placeholder="Enter your Username">
            <label for="password">Password</label>
            <input type="text" id="password" value="" class="form-control" placeholder="Enter your Password">
            <input type="submit" class="btn btn-primary" value="Login" id="login">
        </fieldset>
    </legend>
</form>

1 个答案:

答案 0 :(得分:0)

我更新了我的表单html,将name添加到元素中。它工作正常。

这是更新的html:

<form action="#" id="loginForm" class="form-horizontal" role="form">
    <legend>
        <fieldset>
            <label for="userName">UserName</label>
            <input type="text" value="" id="userName" name="userName" class="form-control" placeholder="Enter your Username">
            <label for="password">Password</label>
            <input type="text" id="password" value="" name="password" class="form-control" placeholder="Enter your Password">
            <input type="submit" class="btn btn-primary" value="Login" id="login">
        </fieldset>
    </legend>
</form>