【问题标题】:How to pass variables to another page?如何将变量传递到另一个页面?
【发布时间】:2014-05-15 12:03:23
【问题描述】:

我实际上是使用下面的代码让它工作的。感谢所有给我意见的人,但不得不说我很难让它正常工作。希望对 javascript 新手有所帮助。

function SelectedRow() {
            (function () {
                if (window.addEventListener) {
                    window.addEventListener('load', run, false);
                } else if (window.attachEvent) {
                    window.attachEvent('onload', run);
                }

                function run() {
                    var table = document.getElementById('alternativeCableTable');
                    table.onclick = function (event) {
                        var target = event.target || event.srcElement;
                        while (target && target.nodeName != 'TR') { 
                            target = target.parentElement;
                        }              
                        var cells = target.cells;              
                        if (!cells.length || target.parentNode.nodeName == 'THEAD') {
                            return;
                        }
                        var stockcode = document.getElementById('stockcode');
                        var wiretype = document.getElementById('wiretype');
                        var wirelength = document.getElementById('wirelength');
                        var terminalA = document.getElementById('termA');
                        var sealA = document.getElementById('sealA');
                        var terminalB = document.getElementById('termB');
                        var sealB = document.getElementById('sealB');
                        stockcode.value = cells[0].innerHTML;
                        wiretype.value = cells[1].innerHTML;
                        wirelength.value = cells[2].innerHTML;
                        terminalA.value = cells[3].innerHTML;
                        sealA.value = cells[4].innerHTML;
                        terminalB.value = cells[5].innerHTML;
                        sealB.value = cells[6].innerHTML;
                        alert("The select data is sent to print page");
                        window.open("http://10.19.13.67/ReworkLabels/PrintLabelPage.aspx?stockcode=" + decodeURIComponent(stockcode.value) + "&wiretype=" + decodeURIComponent(wiretype.value) + "&wirelength=" + decodeURIComponent(wirelength.value)
                       + "&terminalA=" + decodeURIComponent(terminalA.value) + "&sealA=" + decodeURIComponent(sealA.value) + "&terminalB=" + decodeURIComponent(terminalB.value) + "&sealB=" + decodeURIComponent(sealB.value),"_blank");

                    };

                }

            })();
        }

【问题讨论】:

  • 字符串+ 运算符调用toString() 方法。您需要通过某种方式从 f1 获取您实际想要发送的值。
  • 我认为他们可能基本上是cells[0].innerHTML
  • 你需要使用f1.value 而不是f1f2一样

标签: javascript asp.net


【解决方案1】:

使用本地存储,而不是传递 URL 参数和/或 cookie,因为后两者有很多限制。

http://www.webdesignerdepot.com/2013/04/how-to-use-local-storage-for-javascript/中查看如何执行此操作的示例

如果出于某种原因在服务器端需要这些值,则可以利用 JSON 序列化将值作为 POST 参数传递。 (或者只是带有隐藏输入的 HTML 表单;不要忘记将 name 属性放在需要提交到服务器的 html 表单元素上。)

编辑:在第二次阅读您的问题时,您似乎只需要 2 个简单的字符串值。在这种情况下,检查两个输入元素的 value 属性。 https://developer.mozilla.org/en/docs/Web/API/HTMLInputElement

您可能还想为这些 GET 参数命名,例如

window.open("http://Testing/ReworkLabels/Default.aspx?f1="+encodeURIComponent(f1.value)+"&f2="+encodeURIComponent(f2.value),"my window");

有关 encodeURIComponent 函数的更多信息,请参阅https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent

请参阅Retrieve GET variables from URL in ASPX 以在目标 ASP.NET 页面中读取这些参数。

同时确保不直接打印它们的值(但正确转义),以避免跨站点脚本 (XSS) 漏洞。

http://msdn.microsoft.com/en-us/library/ff649310.aspxHow do you avoid XSS vulnerabilities in ASP.Net (MVC)?

【讨论】:

    【解决方案2】:

    试试

     window.open("http://Testing/ReworkLabels/Default.aspx?"+f1.value+"&"+f2.value,"my window");
    

    改为。

    【讨论】:

    • 感谢它确实有效,现在我在 Default.aspx 中有两个文本框,我需要知道如何将 f1.value 放入 textbox1 并将 f2.value 放入 textbox2?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-15
    • 2019-03-06
    • 2015-12-17
    • 1970-01-01
    相关资源
    最近更新 更多