【问题标题】:Passing a variable to a new function (on a new page)将变量传递给新函数(在新页面上)
【发布时间】:2012-05-18 10:12:36
【问题描述】:

在我的第一页上,我有两个单选按钮,每个按钮都有一个值。单击链接转到第 2 页时,将运行以下 for 循环并获取选中按钮的值。 (该脚本有效,我已经测试过了):

function func1(){
    var info = document.Information;
    for(var i1 = 0;i1 < info.length;i1++){
        if (info.elements[i1].type == "radio" && info.elements[i1].checked) {
        ordertype = info.elements[i1].value;
        alert(ordertype);       
        }
    }
}

变量 ordertype 在函数外部声明。在第二页上,我通过单击按钮运行此功能:

function func2(){
     alert(ordertype);  
}

ordertype 现在未定义...如何正确地将变量传递给新函数?

【问题讨论】:

  • 页面之间不共享 JavaScript 执行上下文。
  • 饼干?会议?本地存储?查询字符串?
  • 第一页用window.open打开第二页了吗?

标签: javascript html function variables


【解决方案1】:

如果使用 javascript... 你可以通过查询字符串传递:

function func1() {

        var info = document.Information;

        for (var i1 = 0; i1 < info.length; i1++) {
            if (info.elements[i1].type == "radio" && info.elements[i1].checked) {
                ordertype = info.elements[i1].value;
                break;
            }
        }
        location = 'page.html?value=' + ordertype;
    }

或者只是在表单中添加一个提交按钮。

and method=post to you form-tag,您将在不使用 javascript 的情况下获得查询字符串中所选单选按钮的值?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-15
    • 1970-01-01
    • 2011-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    相关资源
    最近更新 更多