使用 jQuery 创建表单形式的查询字符串不起作用
Create a querystring out of form with jQuery isn't working
我想发送一个ajax请求,但我尝试构建的查询字符串为空。
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>This is a project to show how to use RESTful</title>
</head>
<body>
<script type="text/javascript">var contexPath = "<%=request.getContextPath()%>";</script>
<script src="<%=request.getContextPath()%>/js/jquery.js"></script>
<script type="text/javascript">
function doAjaxPost() {
var queryString = $('#htmlform') // empty
alert("doAjaxPost Called:" + queryString + ":");
$.ajax({
...
...
});
} </script>
<H1>Add Employee</H1>
<p>
<form name="htmlform">
<table border=1>
<thead><tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
</tr></thead>
<tr>
<td><input type="text" name="ID" maxlength="5" size="3"></td>
<td><input type="text" name="Name" maxlength="10" size="10"></td>
<td><input type="text" name="Email" maxlength="10" size="10"></td>
</tr>
</table>
<input type="button" value="Save Employee" onclick="doAjaxPost();" />
<p>
<p>
</form>
[<a href="http://localhost:8080/RESTful/service/employees">List all Employees</a> | <a href="add.jsp">Employee Form Test</a>]
</body>
</html>
更改自:
<form name="htmlform">
自:
<form id="htmlform">
和来自:
var queryString = $('#htmlform')
自:
var queryString = $('#htmlform').serialize();
你正在使用jQuery选择一个HTML对象,但你没有从中得到任何东西(当你将它附加到url字符串时,queryString仍然是一个jQuery对象)。
尝试以下操作
var queryString = $('#htmlform').serialize();
这应该会导致表单被序列化为字符串,此时追加它应该可以正常工作。
您需要
在表单上说id="htmlform"
才能使$("#htmlform")
正常工作。
相关文章:
- 如何有效地将游戏数据存储在URL查询字符串中
- 从查询字符串参数推断出正确的数据类型
- AngularJS:获取链接中没有哈希的查询字符串值
- AngularJS,如何更改查询字符串
- 如何将activeadmin查询字符串传递给batch_action
- 如何从查询字符串中的输入字段发回文本
- Django在使用AJAX时没有接收到新的查询字符串
- 使用查询字符串方法清除浏览器缓存
- 用于刷新的Javascript更改查询字符串
- 我想将查询字符串变量添加到JSON名称/值对中
- 将查询字符串值从页面传递 asp.net 到 iframe
- jqueryajax生成查询字符串,如果未定义则不通过
- 如何将查询字符串传递到HTML框架
- AngularJs将查询字符串发布到c#控制器
- Jquery - 从查询字符串中获取“#”值之后,而不是在“?”符号值之后获取
- 在 JavaScript 中将查询字符串传递给 Web 服务 (ASP.NET)
- 在查询字符串中查找 where 子句
- POST encodeURI查询字符串内部服务器错误 500
- JavaScript:从具有使用 # 符号的路由的 URL 获取查询字符串
- 更改链接 URL 中的路径名,同时保持查询字符串不变