发布到当前页面,保持查询字符串值

时间:2013-01-23 14:43:52

标签: asp-classic

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

我该怎么做?

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

2 个答案:

答案 0 :(得分:3)

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

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

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

HTH, 埃里克

答案 1 :(得分:-1)

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 Classic解决方案,则需要使用函数从查询字符串中清除旧值 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代码段中,请使用以下答案: 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