表单序列化-可以简化表单提交数据

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了表单序列化-可以简化表单提交数据相关的知识,希望对你有一定的参考价值。

提交表单的时候,可以用表单序列化的方式,就不用找到每个要提交的id逐个提交!

<form id="loginInputForm" method="post">
<table>
<tr>
<th align="right">用户名</th><td><input name="name"/></td>
</tr>
<tr>
<th align="right">密码</th><td><input name="password" type="password"/></td>
</tr>
</table>
</form>

表单提交:(一般的提交方式)

$.ajax({
url:‘‘,
data:{
         name:$(‘#loginInputForm input[name=name]‘).val(),
         password:$(‘#loginInputForm input[name=password]‘).val()
      },
      dataType:‘json‘,
});

在ajax中提交的时候,可以直接用$(‘#loginInputForm‘).serialize(),就可以将每个要提交的id的内容提交
$.ajax({
    url:‘‘,
    data:$(‘#loginInputForm‘).serialize(),

    dataType:‘json‘,

});

以上是关于表单序列化-可以简化表单提交数据的主要内容,如果未能解决你的问题,请参考以下文章