javascript 将文本框的值传递给另一个页面 html
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22389883/
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
Passing values of textbox to another page html
提问by user3403327
Hi guys this may be a silly question but if I have a html page open that contains a textbox is it possible to move the value of that textbox to another page onces a button is clicked?
嗨,伙计们,这可能是一个愚蠢的问题,但是如果我打开了一个包含文本框的 html 页面,是否可以在单击按钮后将该文本框的值移动到另一个页面?
So "page1.html" has a textbox called "TextboxName", User types in name "John", button is clicked and then that Page is closed and "John" is moved to the textbox on "page2.html"
所以“page1.html”有一个名为“TextboxName”的文本框,用户输入名称“John”,点击按钮,然后页面关闭,“John”移动到“page2.html”上的文本框
Thanks in advance for any help this problem really has stumped me!
在此先感谢您的帮助,这个问题真的让我很难过!
回答by Jatin
Try Below Code:
试试下面的代码:
Page1.html:
第1页.html:
<html>
<form type=get action="page2.html">
<table>
<tr>
<td>First Name:</td>
<td><input type=text name=firstname size=10></td>
</tr>
<tr>
<td>Last Name:</td>
<td><input type=text name=lastname size=10></td>
</tr>
<tr>
<td>Age:</td>
<td><input type=text name=age size=10></td>
</tr>
<tr>
<td colspan=2><input type=submit value="Submit">
</td>
</tr>
</table>
</form>
</html>
Page2.html:
Page2.html:
<html>
<script LANGUAGE="JavaScript">
function getParams(){
var idx = document.URL.indexOf('?');
var params = new Array();
if (idx != -1) {
var pairs = document.URL.substring(idx+1, document.URL.length).split('&');
for (var i=0; i<pairs.length; i++){
nameVal = pairs[i].split('=');
params[nameVal[0]] = nameVal[1];
}
}
return params;
}
params = getParams();
firstname = unescape(params["firstname"]);
lastname = unescape(params["lastname"]);
age = unescape(params["age"]);
document.write("firstname = " + firstname + "<br>");
document.write("lastname = " + lastname + "<br>");
document.write("age = " + age + "<br>");
</script>
</html>
回答by Thomas Junk
That depends on how much effort and work you want to put into your project.
这取决于您想在项目中投入多少精力和工作。
One easy -but not secure- solution is possible via querystrings. You could pass via HTTP-GET
any parameter from A to B.
通过querystrings可以实现一种简单但不安全的解决方案。您可以将HTTP-GET
任何参数从 A传递到 B。
Hence it is provided als plaintext, it is obvious, that this method is not very safe. Anybody could change the input at any given time. And the result depends on the malicious potential of your attacker. Look here for a simple introduction to Cross Site Scripting (XSS).
因此它以明文形式提供,很明显,这种方法不是很安全。任何人都可以在任何给定时间更改输入。结果取决于攻击者的恶意潜力。在这里查看对跨站点脚本 (XSS)的简单介绍。
Another possibility is via HTTP-Post
(See Specification).
另一种可能性是通过HTTP-Post
(见规范)。
Then it depends on which backend you are using. The backend receives the data from A and renders it to B.
然后这取决于您使用的后端。后端从 A 接收数据并将其呈现给 B。
回答by simonmoonlandings
This can be done, but it requires something other than just plain html. Javascript or PHP could easily accomplish such a function.
这可以做到,但它需要的不仅仅是普通的 html。Javascript 或 PHP 可以轻松完成这样的功能。
See this: How to pass variables from one php page to another without form?
请参阅:如何在没有表单的情况下将变量从一个 php 页面传递到另一个页面?
Or try javascript + AJAX: http://www.w3schools.com/ajax/ajax_intro.asp
或尝试 javascript + AJAX:http: //www.w3schools.com/ajax/ajax_intro.asp
and: http://www.javascripter.net/faq/passingp.htm
和:http: //www.javascripter.net/faq/passingp.htm
You could also store the information in a cookie and access it that way: http://www.javascripter.net/faq/cookies.htm
您还可以将信息存储在 cookie 中并以这种方式访问它:http: //www.javascripter.net/faq/cookies.htm
or just use angular: http://docs.angularjs.org/guide/databinding
或者只是使用角度:http: //docs.angularjs.org/guide/databinding
回答by Preston S
On page1.html do:
在 page1.html 上做:
function onClickHandler(){
location.href = 'page2.html?name=' + document.getElementById('TextBoxName').value;
}
then on page2.html do:
然后在 page2.html 上做:
function parseQueryString()
{
var queryDict = {}
location.search.substr(1).split("&").forEach(function(item) {
queryDict[item.split("=")[0]] = item.split("=")[1]
});
return queryDict;
}
function onLoadHandler(){
document.getElementById('TextBoxName').value = parseQueryString().name;
}