今天在研究jquery用ajax提交form表单中得数据时,学习到了一种新的提交方式
[size=large][b]jquery中的serialize() 方法[/b][/size]
该方法通过序列化表单值,创建 URL 编码文本字符串
序列化的值可在生成 AJAX 请求时用于 URL 查询字符串中
这样当表单中要提交的参数比较多时,就可以使用该方法进行提交,否则将在ajax中得代码会很长,有可能在编写时出错,也不方便检查
以下是自己写的测试代码
[size=large][b]jquery中的serialize() 方法[/b][/size]
该方法通过序列化表单值,创建 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>