在javascript中将参数传递给URL

时间:2013-09-25 14:01:08

标签: javascript jquery

我的DIV中有.ASPX,当我点击此DIV时,我会在我的javascript代码中触发此内容。

$("#btnFiltrar").click(function () {
    var status = "";
    var tipo = "";
    var outros = "";
    $("#divStatusImovel input").each(function () {
        if ($(this).is(":checked") == true) {
            var id = $(this).attr("id").replace("status_", "");
            switch (id) {
                case "1":
                    status += "at=t&";
                    break;
                case "2":
                    status += "in=t&";
                    break;
                case "14":
                    status += "ds=t&";
                    break;
                case "3":
                    status += "vnd=t&";
                    break;
                case "7":
                    status += "vndpr=t&";
                    break;
                case "8":
                    status += "vndtr=t&";
                    break;
                case "4":
                    status += "vndtr=t&";
                    break;
                case "9":
                    status += "vndtr=t&";
                    break;
            }
        }
    });

    $("#divTipoImovel input").each(function () {
        if ($(this).is(":checked") == true) {
            var id = $(this).attr("id").replace("tipo_", "");
            switch (id) {
                case "1":
                    tipo += "t1=t&";
                    break;
                case "2":
                    tipo += "t2=t&";
                    break;
                case "3":
                    tipo += "t3=t&";
                    break;
                case "4":
                    tipo += "t4=t&";
                    break;
                case "5":
                    tipo += "t5=t&";
                    break;
                case "7":
                    tipo += "t7=t&";
                    break;
                case "8":
                    tipo += "t8=t&";
                    break;
                case "9":
                    tipo += "t9=t&";
                    break;
                case "14":
                    tipo += "t14=t&";
                    break;
                case "15":
                    tipo += "t15=t&";
                    break;
                case "17":
                    tipo += "t17=t&";
                    break;
                case "145":
                    tipo += "t145=t&";
                    break;
            }
        }
    });

    $("#divOutrosTipos input").each(function () {
        if ($(this).is(":checked") == true) {
            var id = $(this).attr("id").replace("outros_", "");
            switch (id) {
                case "1":
                    outros += "ha=t&";
                    break;
                case "2":
                    outros += "fa=t&";
                    break;
                case "3":
                    outros += "fo=t&";
                    break;
                case "4":
                    outros += "pl=t&";
                    break;
                case "5":
                    outros += "dce=t&";
                    break;
            }
        }
    });
    var pathname = window.location;
    pathname += "?" + status + tipo + outros;
    pathname = pathname.substring(0, pathname.length - 1);
    alert(pathname);
    window.location = pathname;
});

在这个javascript函数的最后一行中,我尝试将用户带到同一页面,但是在我的URL上传递了一些参数但没有成功,在我的页面中重新加载但不传递我的URL上的参数。

有人可以帮助我吗?

更新

好奇心,当我在debugger之前放置window.location.href

var pathname = window.location;
pathname += "?" + status + tipo + outros;
pathname = pathname.substring(0, pathname.length - 1);
debugger;
window.location.href = pathname;

3 个答案:

答案 0 :(得分:0)

如果你想读取传递给URL的变量,这可能会帮助你,这就是我使用的。

    function getQueryVariable(variable) {
        var query = window.location.search.substring(1);
        var vars = query.split("&");
         for (var i = 0; i < vars.length; i++) {
            var pair = vars[i].split("=");
            if (pair[0] == variable) { return pair[1]; }
         }
         return (false);
    }

例如url可能是这样的: www.example.com?area=AC&interval=day

然后您可以通过以下方式获取变量: var yourvariable = getQueryVariable(“area”);

答案 1 :(得分:0)

根据要求,我将一个带有一些测试标记的jsFiddle放在一起:

<div id="divStatusImovel">
    <input type="radio" id="status_4" name="status">status_4</input>
    <input type="radio" id="status_1" name="status">status_1</input>
</div>
<br/>
<div id="divTipoImovel">
    <input type="radio" id="tipo_9" name="tipo">tipo_9</input>
    <input type="radio" id="tipo_145" name="tipo">tipo_145</input>
</div>
<button id="btnFiltrar">Submit</button>

我改变了查询字符串的构建方式,最后,您需要做的就是:

var qs_arr = [];
for (prop in qs_items)
    qs_arr.push(prop + "=" + qs_items[prop]);

var qs_str = qs_arr.join("&");

window.location.search = qs_str;

这是因为查询字符串的每个部分都是使用简单的key-&gt;值map(object)构建的,如下所示:

var id = $(this).attr("id").replace("outros_", "");
var id_map = {
    "1" : "ha", "2" : "fa", "3" : "fo",
    "4" : "pl", "5" : "dce"
};
qs_items[id_map[id]] = "t";

请参阅完整的jsFiddle

答案 2 :(得分:0)

尝试改变:

window.location

window.location.href