【问题标题】:Post to current page, keeping querystring values in tact发布到当前页面,保持查询字符串值不变
【发布时间】:2013-01-07 02:07:03
【问题描述】:

我有一个页面,我想创建一个下拉列表,然后发回我现在所在的页面。

我该怎么做?

一个问题是,我希望所有查询字符串的值也相同,除了 1 将覆盖下拉列表。

【问题讨论】:

    标签: asp-classic


    【解决方案1】:

    如果需要,您可以在同一页面中同时使用查询字符串和表单变量。如果您使用<form method="post"> 并将操作留空,它会将表单发送回当前页面,这样就解决了一个问题。有一个警告:我不确定将操作留空是否会使查询字符串参数保持不变。

    如果没有,您可以尝试以下操作:<form method="post" action="index.asp?<%= request.querystring %>">(不确定确切的语法,要点是您需要指定当前页面并在方法中添加当前查询字符串变量)。

    在发布后页面上的 ASP 代码中,您可以检查 request.form 和 request.querystring。 request.form 将包含您的表单发布变量。 request.querystring 将包含后面的变量?在您的网址中。

    HTH, 埃里克

    【讨论】:

    • 空表单操作不保留当前 url 查询字符串
    【解决方案2】:

    一个Javascript方法:

    <html>
    <head>
    <script>
    function jumpto(whatform, querykey) {
        //get the url querystring
        var url = window.location.search;
        //the replace query
        var queryrx = new RegExp("([?&])" + querykey + "=[^\&]+(?=&|$)", "gi");
        //which item selected in dropdown
        var index=whatform.pageselect.selectedIndex;
        //if the first option, ignore it since it is blank
        if (whatform.pageselect.options[index].value != "0") {
            //is a query string available
            if (url.length>0) {
                //our query key is present
                if (queryrx.test(url)) {
                    //now we replace the querystring from old to new
                    url = url.replace(queryrx, '$1' + querykey + '='+whatform.pageselect.options[index].value);
                    //clear out the question mark from the querystring
                    url = url.replace("?", '');
                //our query key is not present, but there is querystring data
                }else{
                    url = url.replace("?", '');
                    url = querykey + "=" + whatform.pageselect.options[index].value + "&" + url;
    
                }
            //no querystring data exists
            }else{
                url = querykey + "=" + whatform.pageselect.options[index].value;
            }
            //alert(url);
            //this is the url we are getting bounced to
            location = "mypage.asp?"+url;
        }
    }
    </script>
    </head>
    <body>
    <FORM NAME="form1">
    <SELECT NAME="pageselect" ONCHANGE="jumpto(this.form, 'thequerykey')" SIZE="1">
    <OPTION VALUE="">Choose a Page</OPTION>
    <OPTION VALUE="pageA">First Page</OPTION>
    <OPTION VALUE="pageB">Second Page</OPTION>
    </SELECT>
    </FORM>
    </body>
    </html>
    

    如果您想使用 ASP 经典解决方案,您将需要使用函数从查询字符串中清除旧值 https://stackoverflow.com/a/1221672/2004151 然后将您的查询字符串打印为表单中的隐藏输入字段(MyFunctionResultsExceptPageSelect 下面)。 比如:

    <FORM ACTION="mypage.asp" METHOD="GET" NAME="form3">
    <%=MyFunctionResultsExceptPageSelect("pageselect")%>
    <SELECT NAME="pageselect" ONCHANGE="document.form3.submit()" SIZE="1">
    <OPTION VALUE="">Choose a Page</OPTION>
    <OPTION VALUE="pageA">First Page</OPTION>
    <OPTION VALUE="pageB">Second Page</OPTION>
    </SELECT>
    </FORM>
    
    <%
    Function MyFunctionResultsExceptPageSelect(key)
    
      Dim qs, x
    
      For Each x In Request.QueryString
        If x <> key Then
            qs = qs & "<input type=""hidden"" name="""&x&""" value="""&Request.QueryString(x)&""" />"
        End If
      Next
    
      MyFunctionResultsExceptPageSelect = qs
    
    End Function
    %>
    

    如果您想获取当前页面而不是手动指定它,请使用以下内容。 在 javascript sn-p 中,在此处使用答案: https://stackoverflow.com/a/5817566/2004151 在 ASP 中,是这样的: http://classicasp.aspfaq.com/files/directories-fso/how-do-i-get-the-name-of-the-current-url/page.html

    【讨论】:

      猜你喜欢
      • 2010-12-03
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多