您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

Ajax方法实现Form表单提交的方法

2025/3/6 14:56:20发布33次查看
这次给大家带来ajax方法实现form表单提交的方法,ajax方法实现form表单提交的注意事项有哪些,下面就是实战案例,一起来看一下。
写在前面的话
在使用form表单的时候,一旦点击提交触发submit事件,一般会使得页面跳转,页面间的跳转等行为的控制权往往在后端,后端会控制页面的跳转及数据传递,但是在某些时候不希望页面跳转,或者说想要将控制权放在前端,通过js来操作页面的跳转或者数据变化。
一般这种异步的操作,我们都会想到ajax方式,因此在实现了功能后就整理了这篇文章,通过ajax方法实现form表单的提交并进行后续的异步操作。
常见的form表单提交方式
<!doctype html public "-//w3c//dtd html 4.01 transitional//en"> <html> <head>   <title>login test</title>   <meta http-equiv="content-type" content="text/html; charset=utf-8">   <meta http-equiv="pragma" content="no-cache">   <meta http-equiv="cache-control" content="no-cache">   <meta http-equiv="expires" content="0">   <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">   <meta http-equiv="description" content="login test">   </head> <body> <p id="form-p">   <form id="form1" action="/users/login" method="post">     <p>用户名:<input name="username" type="text" id="txtusername" tabindex="1" size="15" value=""/></p>     <p>密 码:<input name="password" type="password" id="textbox2" tabindex="2" size="16" value=""/></p>     <p><input type="submit" value="登录"> <input type="reset" value="重置"></p>   </form> </p> </body> </html>
点击登录按钮后,即触发form表单的提交事件,数据传输至后端,由后端控制页面跳转和数据。
ajax实现form提交方式
修改完成后代码如下:
<!doctype html public "-//w3c//dtd html 4.01 transitional//en"> <html> <head>   <title>login test</title>   <meta http-equiv="content-type" content="text/html; charset=utf-8">   <meta http-equiv="pragma" content="no-cache">   <meta http-equiv="cache-control" content="no-cache">   <meta http-equiv="expires" content="0">   <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">   <meta http-equiv="description" content="ajax方式">   <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>   <script type="text/javascript">     function login() {       $.ajax({       //几个参数需要注意一下         type: post,//方法类型         datatype: json,//服务端接收的数据类型         url: /users/login ,//url         data: $('#form1').serialize(),         success: function (result) {           console.log(result);//打印服务端返回的数据(调试用)           if (result.resultcode == 200) {             alert(success);           }           ;         },         error : function() {           alert(异常!);         }       });     }   </script> </head> <body> <p id="form-p">   <form id="form1" onsubmit="return false" action="##" method="post">     <p>用户名:<input name="username" type="text" id="txtusername" tabindex="1" size="15" value=""/></p>     <p>密 码:<input name="password" type="password" id="textbox2" tabindex="2" size="16" value=""/></p>     <p><input type="button" value="登录" onclick="login()"> <input type="reset" value="重置"></p>   </form> </p> </body> </html>
注意事项
在常用方式中,点击的登录按钮的type为submit类型;
在常用方式中,form的action不为空;
ajax方式中需要注意的是$.ajax方法中的参数:datatype和data。
我平时很少写前端代码,级别也就是入门级别,能看懂能改而已,所以很多时候都是百度,像这次这个功能的实现也是借助了百度,但是,我百度到的代码在$.ajax方法中设置的datatype参数值为html而不是json,导致我在一开始调试的时候一直报错,最终是改成了json才成功,因此在这里特别说明并提醒一下,别和我一样走错了路,还有就是向服务端传输的data值了,像上面代码一样,将form表单中的数据序列化传输即可。
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
ajax怎么进行异步请求
ajax实现跨域请求的步骤详解
以上就是ajax方法实现form表单提交的方法的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product