【发布时间】:2013-10-20 10:54:47
【问题描述】:
我正在尝试使用空操作从 from 发送帖子数据,并使用 javascript 将信息发送到必须处理帖子信息的文件。
这是我的代码:
<HTML>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
</head>
<script>
function post_to_url(path, params, method) {
method = method || "post";
var form = document.createElement("form");
form._submit_function_ = form.submit;
form.setAttribute("method", method);
form.setAttribute("action", path);
for(var key in params) {
var hiddenField = document.createElement("input");
hiddenField.setAttribute("type", "hidden");
hiddenField.setAttribute("name", key);
hiddenField.setAttribute("value", params[key]);
form.appendChild(hiddenField);
}
document.body.appendChild(form);
form._submit_function_();
}
post_to_url("./postinfo.php", { submit: "submit" } );
</script>
<form method="post" onsubmit="return post_to_url()">
<input type="text" name="ime">
<input type="submit" id="submit" name="submit" value="Send">
</form>
那么我如何在我的 html 表单中使用带有空操作的 javascript 将帖子数据发送到 postinfo.php?
提前致谢!
【问题讨论】:
-
在您的代码中,您有 2 次调用
post_to_url- 一次发生在您定义函数之后,一次发生在提交表单时。我怀疑这可能会导致问题。
标签: javascript html forms post