今天在研究jquery用ajax提交form表单中得数据时,学习到了一种新的提交方式
jquery中的serialize() 方法
该方法通过序列化表单值,创建 URL 编码文本字符串
序列化的值可在生成 AJAX 请求时用于 URL 查询字符串中
这样当表单中要提交的参数比较多时,就可以使用该方法进行提交,否则将在ajax中得代码会很长,有可能在编写时出错,也不方便检查
以下是自己写的测试代码
<script type="text/javascript" src="/js/jquery.js"></script> <script type="text/javascript"> $().ready(function() { $("#submit").click(function() { $.ajax({ url : "insertUser", data : $("form").serialize(), type : "POST", dataType : "text", complete : function() {location.href = "main.html"} }); } } </script>
<body> <form name="insertUser" method="post" action="main"> 用户名:<input type="text" name="userId" id="userId" /> 密码:<input type="password" name="staticPassword" id="staticPassword" /> <input type="button" id="submit" value="提交" /> </form> </body>
1 楼
shuijingping
2011-11-21
我用了你的方法,但是取不到返回来的数据,为啥呢?
2 楼
Edward_Lee
2011-12-01
shuijingping 写道
我用了你的方法,但是取不到返回来的数据,为啥呢?
有报什么错误或者其它信息吗?
注意提交按钮type要设为"button"
你想得到什么样的返回值?会不会你同一个页面有多个form?