admin管理员组文章数量:1415100
I have this form:
<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>
And this js:
$( "form" ).submit(function( event ) {
console.log( $( this ).serializeArray() );
event.preventDefault();
});
But it only returns the csrf tokens twice. Any idea why this is happing and how can I fix it?
*Some text to stop this post been mostly code! *More text to stop this post been mostly code!
I have this form:
<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>
And this js:
$( "form" ).submit(function( event ) {
console.log( $( this ).serializeArray() );
event.preventDefault();
});
But it only returns the csrf tokens twice. Any idea why this is happing and how can I fix it?
*Some text to stop this post been mostly code! *More text to stop this post been mostly code!
Share Improve this question edited May 12, 2017 at 17:05 Abdullah Seba asked May 12, 2017 at 16:54 Abdullah SebaAbdullah Seba 3442 gold badges6 silver badges16 bronze badges 2- What other values are you expecting? – Lennholm Commented May 12, 2017 at 16:58
- @MikaelLennholm Depends what the user entered in the email and password fields. – Abdullah Seba Commented May 12, 2017 at 17:00
3 Answers
Reset to default 3You haven't given the email and password inputs proper names, that's why the are excluded from the serialization.
Give those input elements the names you want them to be serialized with and you should be good.
Here is your original code in a JS Fiddle instance, and a corrected version with the name attributes filled in. The only differences are specified below:
Original Inputs
<input
id="password"
type="password"
value="my-password"
style=" border: 0; background: transparent;"
class="form-control"
/>
<!-- snip -->
<input
id="email"
type="email"
value="[email protected]"
style=" border: 0; background: transparent;"
class="form-control"
/>
Corrected Inputs
<input
id="password"
type="password"
name="password"
value="my-password"
style=" border: 0; background: transparent;"
class="form-control"
/>
<!-- snip -->
<input
id="email"
type="email"
name="email"
value="[email protected]"
style=" border: 0; background: transparent;"
class="form-control"
/>
Your input fields have an empty name attribute. Per documentation
The .serializeArray() method uses the standard W3C rules for successful controls to determine which elements it should include; in particular the element cannot be disabled and must contain a name attribute.
https://api.jquery./serializeArray/
The problem is that you have empty name
attributes. According to the JQuery .serializeArray() Documentation:
The .serializeArray() method uses the standard W3C rules for successful controls to determine which elements it should include; in particular the element cannot be disabled and must contain a name attribute.
You can fix it by adding name attributes for your fields:
<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">
本文标签: javascriptserializeArray() not getting all fieldsStack Overflow
版权声明:本文标题:javascript - `serializeArray()` not getting all fields - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745164345a2645600.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论