简体   繁体   English

使用复选框中的window.location.assign将多个GET参数添加到URL

[英]Adding multiple GET Parameters to URL using window.location.assign from checkboxes

I'm trying to use JavaScript to add parameters to the current URL in order to filter WordPress posts. 我正在尝试使用JavaScript将参数添加到当前URL,以便过滤WordPress帖子。 Example: http://domain.com/houses/?rooms=1,2&bath=1&yard=yes 例如: http://domain.com/houses/?rooms=1,2&bath=1&yard=yeshttp://domain.com/houses/?rooms=1,2&bath=1&yard=yes

I have successfully done this with one of the parameters, but I'm failing in understanding how to add on multiple parameters. 我已经使用参数之一成功完成了此操作,但未能理解如何添加多个参数。 What is the proper way of getting this one? 得到这个的正确方法是什么?

Also, is using window.location.assign() , the proper way for adding the parameters to the URL? 另外,是否使用window.location.assign() ,将参数添加到URL的正确方法? Is using AJAX better/safer? 使用AJAX更好/更安全吗?

Below is my code 下面是我的代码

HTML 的HTML

<div id="filter-rooms">
       <ul>
           <li>
               <input type="checkbox" value="1" id="rooms" />1 Room</li>
           <li>
               <input type="checkbox" value="2" id="rooms" />2 Rooms</li>
           <li>
               <input type="checkbox" value="3" id="rooms" />3 Rooms</li>
           <li>
               <input type="checkbox" value="4" id="rooms" />4 Rooms</li>
      </ul>
</div>

<div id="filter-bath">
    <ul>
        <li>
            <input type="checkbox" value="1" id="bath" />1 Bathrooms</li>
        <li>
            <input type="checkbox" value="2" id="bath" />2 Bathrooms</li>
        <li>
            <input type="checkbox" value="3" id="bath" />3 Bathrooms</li>
    </ul>
</div>

<div id="filter-yard">
    <ul>
        <li>
            <input type="radio" name="yard" value="yes" id="yard" />Yes</li>
        <li>
            <input type="radio" name="yard" value="no" id="yard" />No</li>
    </ul>
</div>

JavaScript 的JavaScript

$('#filter-rooms').on('change', 'input[type="checkbox"]', function () {

    var $ul = $(this).closest('ul'),
        vals = [];

    $ul.find('input:checked').each(function () {

        vals.push($(this).val());

    });

    vals = vals.join(",");

    window.location.assign('?rooms=' + vals);

});

Now, in the JavaScript displayed above, I managed to get the rooms parameter successfully working, but when I duplicate this for the other parameters, it replaces the URL with the rooms parameter instead of adding it on to the end of the URL like so - &bath=1&yard=yes 现在,在上面显示的JavaScript中,我设法使rooms参数成功运行,但是当我为其他参数复制此参数时,它将URL替换为rooms参数,而不是像这样将其添加到URL的末尾- &bath=1&yard=yes

jsFiddle: http://jsfiddle.net/g9Laforb/1/ (Note: window.location.assign() is missing because jsFiddle doesn't allow it.) jsFiddle: http : //jsfiddle.net/g9Laforb/1/ (注意:因为jsFiddle不允许,所以缺少window.location.assign()。)

Any advice on this would be greatly appreciated. 任何对此的建议将不胜感激。

First, do not use the same id on multiple elements. 首先,请勿在多个元素上使用相同的id See Why is it a bad thing to have multiple HTML elements with the same id attribute? 请参阅为什么具有多个具有相同id属性的HTML元素是一件坏事?

Your problem is with this line 你的问题是这条线

 window.location.assign('?rooms=' + vals);

You need to dynamically update the query string. 您需要动态更新查询字符串。

Using this method 使用这种方法

function updateQueryStringParameter(uri, key, value) {
  var re = new RegExp("([?&])" + key + "=.*?(&|$)", "i");
  var separator = uri.indexOf('?') !== -1 ? "&" : "?";
  if (uri.match(re)) {
    return uri.replace(re, '$1' + key + "=" + value + '$2');
  }
  else {
    return uri + separator + key + "=" + value;
  }
}

from add or update query string parameter 添加或更新查询字符串参数

and custom attributes like 和自定义属性,例如

<ul data-type="rooms">

you can do 你可以做

window.location.href = updateQueryStringParameter(window.location.href, $ul.attr('data-type'), vals);

instead of 代替

window.location.assign('?rooms=' + vals);

Try this : define variables to store rooms, bath and yard selected values and create url accordingly in each change handler of inputs. 尝试以下操作:定义变量以存储房间,浴室和院子中选定的值,并在每个输入更改处理程序中相应地创建url。

roomVal = '';
bathVal = '';
yardVal = '';

$('#filter-rooms').on('change', 'input[type="checkbox"]', function () {
     roomVal = $(this).closest('ul').find('input[type="checkbox"]:checked').map(function(i,v){
        return $(this).val();
    }).get();

    window.location.assign('?rooms=' + roomVal + '&bath='+bathVal+"&yard="+yardVal);
});

$('#filter-bath').on('change', 'input[type="checkbox"]', function () {
     bathVal = $(this).closest('ul').find('input[type="checkbox"]:checked').map(function(i,v){
        return $(this).val();
    }).get();

    window.location.assign('?rooms=' + roomVal + '&bath='+bathVal+"&yard="+yardVal);
});

$('#filter-yard').on('change', 'input[type="radio"]', function () {
     yardVal = $(this).closest('ul').find('input[type="radio"]:checked').map(function(i,v){
        return $(this).val();
    }).get();

    window.location.assign('?rooms=' + roomVal + '&bath='+bathVal+"&yard="+yardVal);
});

JSFiddle Demo JSFiddle演示

You can simply do it with window.location.href If you have any value stored in a variable , you can use the + symbol to concatenate the value. 您可以简单地使用window.location.href如果variable存储了任何值,则可以使用+符号将其连接起来。

var somedata = somedata;

window.location.href = "http://www.yourwebsite.com?data1=somedata&data2="+somedata;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM