【问题标题】:How to pass a text box value from one html page to another如何将文本框值从一个html页面传递到另一个
【发布时间】:2017-03-25 18:40:38
【问题描述】:

我想将文本框值从一个 html 页面传递到另一个 html 页面并在第二个页面上打印。我有 page1.html 和 page2.html。我不想使用任何服务器端脚本语言的 asp 或 php 只是我想使用 javascript 或 jquery 任何人,这很容易。

<script>
        function checkPassword() {
            if (document.getElementById("name").value == "") {
                document.getElementById("studname").innerHTML = "Enter your name. Field cannot be left blank.";
                alert('Enter your name.');
                return false;
            }
            else if (document.getElementById("class").value == "select") {
                document.getElementById("classname").innerHTML = "Select your class.";
                alert('Select your class.');
                return false;
            }
            else if (document.getElementById("section").value == "select") {
                document.getElementById("secname").innerHTML = "Select your section.";
                alert('Select your section.');
                return false;
            }
            else if (document.getElementById("password").value == "") {
                document.getElementById("passwordname").innerHTML = "Enter your password.";
                alert('Enter your password.');
                return false;
            }
            else if (document.getElementById('password').value == '12345' && document.getElementById("class").value == 'V' && document.getElementById("section").value == 'a') {
                location.href = 'Start.html?name=' + document.getElementById('name').value + '?class=' + document.getElementById('class').value;
            } 
            else {
                alert('Your Class, Section and Password doesn\'t match. Please re-enter correctly.');
                return false;
            }
        }
      </script>

【问题讨论】:

  • 查询字符串,LocalStorage。 ?

标签: javascript jquery html


【解决方案1】:

运行这段代码,你会得到你需要的输出。

html1.html

 <html>
<form type=get action="html2.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>

html2.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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-13
    • 1970-01-01
    • 2011-11-08
    • 2016-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多