`serializeArray()`没有得到所有字段

时间:2017-05-12 16:54:28

标签: javascript jquery html

我有这样的表格:

<form class="js-form">
   <input type="hidden" name="csrf_name" value="csrf5915b96bceb5e">
   <input type="hidden" name="csrf_value" value="b6de78b37c878cfb3cbe2b11edc86049">
   <div class="js-form-alerts">
   </div>
   <div class="js-form-autologin">
      <table class="table table-striped">
         <thead>
            <tr>
               <th>Email</th>
               <th>Password</th>
            </tr>
         </thead>
         <tbody id="table-body">
            <tr>
               <th> <input type="email" name="" value="" style=" border: 0; background: transparent;" class="form-control" id="email"> </th>
               <th>
                  <div class="form-group">
                     <div class="input-group"> <input type="password" name="" value="" style=" border: 0; background: transparent;" class="form-control" id="password"> <span class="input-group-btn"> <a href="#" class="btn btn-warning btn-flat"> <i class="fa fa-edit"></i> </a> <a href="#" class="btn btn-danger btn-flat"> <i class="fa fa-minus"></i> </a> <a href="#" class="btn btn-success btn-flat"> <i class="fa fa-plus"></i> </a> </span> </div>
                  </div>
               </th>
            </tr>
         </tbody>
      </table>
   </div>
   <div class="row">
      <div class="col-xs-12 col-sm-6">
         <div class="vert-pad">
            <button type="submit" id="submit" class="btn btn-block btn-lg btn-success">
            Update Emails
            </button>
         </div>
      </div>
      <div class="col-xs-12 col-sm-3 pull-right">
         <div class="vert-pad">
            <button type="button" class="btn btn-block btn-lg btn-link" data-dismiss="modal">Cancel</button>
         </div>
      </div>
   </div>
</form>

这是js:

$( "form" ).submit(function( event ) {
    console.log( $( this ).serializeArray() );
    event.preventDefault();
  });

但它只返回csrf标记两次。 知道为什么会这样,我该怎么办呢?

enter image description here

*阻止这篇文章的一些文字主要是代码! *更多文字停止这篇文章大多是代码!

3 个答案:

答案 0 :(得分:2)

您还没有给出电子邮件和密码输入正确的名称,这就是从序列化中排除的原因。

为这些输入元素提供您希望它们序列化的名称,您应该很好。

答案 1 :(得分:1)

您的输入字段具有空名称属性。每份文件

  

.serializeArray()方法使用标准的W3C规则   成功的控制,以确定它应包括哪些元素;在   特别是该元素不能被禁用,必须包含一个名称   属性。

https://api.jquery.com/serializeArray/

答案 2 :(得分:1)

问题是你有name个空属性。根据{{​​3}}:

  

.serializeArray()方法使用标准的W3C规则   成功的控制,以确定它应包括哪些元素;在   特别是该元素不能被禁用,必须包含一个名称   属性。

您可以通过为字段添加名称属性来修复它:

<input type="email" name="email" value="" style=" border: 0; background: transparent;" class="form-control" id="email">
...
<input type="password" name="password" value="" style=" border: 0; background: transparent;" class="form-control" id="password">