0

我有一个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;
4

3 回答 3

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);
    }

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

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

于 2013-09-25T14:04:50.063 回答
0

尝试改变:

window.location

window.location.href
于 2013-09-26T10:47:29.560 回答
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;

这是因为查询字符串的每个部分都是使用简单的键->值映射(对象)构建的,如下所示:

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

于 2013-09-26T10:36:52.960 回答