您的位置:首页 > 其它

ajax提交整个form表单

2016-09-08 17:14 489 查看
在项目开发中,有时提交form表单时不能单单用action或者jquery的
</pre><pre class=" JScript" name="code" style="white-space: pre-wrap; word-wrap: break-word; widows: 1; overflow: auto; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; padding: 0px; margin-top: 0px; margin-bottom: 0px; line-height: 1.42857; word-break: break-all; color: rgb(51, 51, 51); border: none; list-style: none; outline: none 0px; background-color: rgb(245, 245, 245);">
表单提交方法有三种,主要说下第三种
第一种:用form自带属性<span style="color: rgb(255, 0, 0);"><strong>action</strong></span>提交
第二种:用jquery提交:<span style="line-height: 1.42857; background-color: rgb(240, 240, 240);"><strong><span style="color: rgb(255, 0, 0);">$("#formid").submit();</span></strong></span>
<span style="line-height: 1.42857; background-color: rgb(240, 240, 240);">第三种:用ajax提交:</span>
<span style="line-height: 1.42857; background-color: rgb(240, 240, 240);">但如果form表单中数据很多时,不可能一一列出,只需要用</span><pre class=" JScript" name="code" style="white-space: pre-wrap; word-wrap: break-word; overflow: auto; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; padding: 0px; margin-top: 0px; margin-bottom: 0px; line-height: 1.42857; word-break: break-all; border: none; list-style: none; outline: none 0px; background-color: rgb(245, 245, 245);"><span style="color: rgb(255, 0, 0);"><strong>$('#yourformid').serialize()</strong></span>就可以了
</pre>


举例如下:

$.ajax({
<span style="white-space: pre;">	</span>cache: true,
<span style="white-space: pre;">	</span>type: "POST",
<span style="white-space: pre;">	</span>url:ajaxCallUrl,
<span style="white-space: pre;">	</span><span style="color: rgb(255, 0, 0);"><strong>data:$('#yourformid').serialize(),</strong></span>// 你的formid
<span style="white-space: pre;">	</span>async: false,
<span style="white-space: pre;">	</span>error: function(request) {
<span style="white-space: pre;">		</span>alert("Connection error");
<span style="white-space: pre;">	</span>},
<span style="white-space: pre;">	</span>success: function(data) {
<span style="white-space: pre;">		</span>$("#commonLayout_appcreshi").parent().html(data);\
<span style="white-space: pre;">	</span>}
});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: