javascript 我怎样才能在 html 中从一个页面到另一个页面获取该值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5374729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can i get that value in html from one page to another page
提问by Prabakaran
I want to check password field value after submiting the form by post method.
我想在通过 post 方法提交表单后检查密码字段值。
How can I get that value in HTML page using javascript
如何使用 javascript 在 HTML 页面中获取该值
Example:
例子:
<form method="post" action="check.html">
<input type="Password" value="" name="pwd">
<input type="Submit" value="Submit" name="Submit">
</form>
I want the value of "pwd" in check.html
to check the authorisation.
我想要“pwd”的值check.html
来检查授权。
回答by Ankit
you can try this alos
你可以试试这个
page1.php
页面1.php
<form method="POST" action="page2.php">
<input type="text" name="field" value="Enter value here" />
</form>
page2.php
页面2.php
<form method="POST">
<input type="text" name="field" value="<?php echo($_POST['field']) ?>" />
</form>
also you can do this with cookies also but using cookies is not a good option i guess.
你也可以用 cookie 来做到这一点,但我想使用 cookie 不是一个好的选择。
another option you can use
您可以使用的另一个选项
file1.html
文件1.html
<Form name="frmDefault" action="file2.html" method="get">
<p>Item1: <Input Type="text" name="txtItem1"></p>
<p>Item2: <Input Type="text" name="txtItem2"></p>
<p><Input type="submit"></p>
</Form>
file2.html
文件2.html
<Script Language="Javascript">
<!--//
var arrArgs = location.search.substring(1).split("&");
for (var i=0; i<arrArgs.length; i++) {
document.write ('<p>' + arrArgs[i] + "</p>");
}
//-->
</Script>
回答by vientoho
There is an existing postabout it
For GET, there is a trickable to do it as below
对于 GET,有一个技巧可以做到如下
var $_GET = {};
document.location.search.replace(/\??(?:([^=]+)=([^&]*)&?)/g, function () {
function decode(s) {
return decodeURIComponent(s.split("+").join(" "));
}
$_GET[decode(arguments[1])] = decode(arguments[2]);
});
// display responses
var response = "";
for (var key in $_GET) {
response += key + ": " + $_GET[key] + ";";
}
alert(response);
回答by heximal
<html>
<script type='text/javascript'>
function doSubmit() {
alert(document.getElementById('pwd').value);
document.getElementById('authfrm').submit();
}
</script>
<body>
<form id='authfrm' method='post'>
<input type='text' id='nick' />
<input type='password' id='pwd' />
<input type='button' onclick='doSubmit();' />
</form>
</body>
</html>
but remember, you can't access any values from html page. you need do assign action="check.php" or some other script engine.
但请记住,您无法从 html 页面访问任何值。您需要分配 action="check.php" 或其他一些脚本引擎。
回答by mplungjan
You cannot get at a posted variable in an html page unless your check.html is not really an html page, but a server processed page with a rewrite rule to hide it is not really html
您无法在 html 页面中获取已发布的变量,除非您的 check.html 不是真正的 html 页面,而是具有重写规则以隐藏它的服务器处理的页面不是真正的 html
Some servers will not even allow you to post to html.
有些服务器甚至不允许您发布到 html。
To get at the variable in javascript, you will need to GET the page - this will show the password in plain text in the url of the page.
要获取 javascript 中的变量,您需要获取页面 - 这将在页面的 url 中以纯文本形式显示密码。
I think you want to completely rethink your approach and password protect the directory where your page is in, and use at least basic authentication to get at it depending on how secret you want it to be. If you just want your aunt to not see your page, then set a cookie in the form and check it in check.html
我认为您想完全重新考虑您的方法和密码保护您的页面所在的目录,并至少使用基本身份验证来获取它,具体取决于您希望它的机密程度。如果你只是想让你的阿姨看不到你的页面,那么在表单中设置一个cookie并在check.html中查看