如何从另一个页面的表单中获取数据(JavaScript)
我的英语不够好,所以希望您了解我的问题...
My English is not good enough so I hope you understand what's my problem...
我有一个带有表单的页面"A".当我单击提交"时,我希望它打开/重定向到页面"B",并且要在页面"B"上显示页面"A"中的表单数据...我想使用JavaScript来做到这一点,不在jQuery左右.我尝试使用window.opener
或类似的方法...接下来,显示示例代码.
I have a page 'A' with a form. When I click Submit, I want it to open/redirect to a page 'B', and I want to show on page 'B' the data of the form in page 'A'... I want to do this with JavaScript, not with jQuery or so. I've tried with window.opener
or things like that... Next, the example code is shown.
页面"A":
<html>
<head>
</head>
<body>
<form method="post" name="form1" id="form1">
<input type="text" name="field1">
<input type="submit" name="submit" onclick="window.open('show.html');">
</form>
</body>
</html>
页面"B":
<html>
<head>
</head>
<body>
<script>
var x = opener.document.forms["form1"]["field1"].value;
document.write(x);
</script>
</body>
</html>
页面"B"可以显示页面"A"上的数据非常重要.我不能使用PHP或其他技术...:/
It's really important that page 'B' can show data from the form on page 'A'. I can't use PHP or another technology... :/
希望您能理解我的要求.谢谢!
I hope you understand what I'm asking. Thanks!
**更新
正如Sagar Hani指出的那样,解决方案(至少对我而言)是localStorage.无论如何,感谢回答的人!
The solution (at least for me) is localStorage, as Sagar Hani indicated. Anyway, thanks to people who answered!
- 您永远无法从客户端(使用javascript或任何其他客户端语言)获取 POST 数据
- 您必须使用服务器端语言来获取帖子数据(例如PHP,nodeJs).
- 但是您可以简单地从客户端(javascript)获取 GET 数据.
- Never you can get POST data from client side (using javascript or any other client side language)
- You must use a server side language to get post data (like PHP, nodeJs).
- But you can simply get GET data from client side (javascript).
以下是您想要的确切解决方案.
Following is the exact solution that you want.
页面"A"(a.html)
Page 'A' (a.html)
<html>
<head>
</head>
<body>
<form action="b.html" method="get" name="form1" id="form1">
<input type="text" name="field1">
<input type="submit" name="submit">
</form>
</body>
</html>
页面'B'(b.html)
Page 'B' (b.html)
<script>
alert(findGetParameter("field1"))
function findGetParameter(parameterName) {
var result = null,
tmp = [];
location.search
.substr(1)
.split("&")
.forEach(function (item) {
tmp = item.split("=");
if (tmp[0] === parameterName) result = decodeURIComponent(tmp[1]);
});
return result;
}
</script>