There are 2 filters in the form of selects, when choosing jsom, I pass parameters to the URL.

введите сюда код 

http: // timelate / stuff? dep-id = 158

What can parse the URL in JS so that when the second filter is selected, the parameter is concatenated into the first one, that is,

http: // timelate / stuff? dep-id = 158 & date = 2016-02-19

Well, if you change one of them so that the values ​​change and all other parameters remain!

 $('#dep-filter').change(function(){ $id = $(this).val(); location.href = '/stuff?dep-id=' + $id; }); $('#month-filter').change(function(){ $date = $(this).val(); location.href = '/stuff?month=' + $date; }); 
  • I did not understand anything, but for parsing the URL, regular expressions are usually used - user205867

1 answer 1

like so

 $(function() { var array = []; var server = 'http://timelate/stuff?'; var url; $('#dep-filter').change(function() { array[0] = ('id=' + $(this).val()); url = server + array.join('&'); $('#res').text(url); }); $('#month-filter').change(function() { array[1] = ('date=' + $(this).val()); url = server + array.join('&'); $('#res').text(url); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <select name="dep-id" id="dep-filter"> <option value="35">35</option> <option value="44">44</option> <option value="546">546</option> </select> <select name="date" id="month-filter"> <option value="2016-02-19">2016</option> <option value="2015-02-19">2015</option> <option value="2017-02-19">2017</option> </select> <div id="res"></div> 

  • That is necessary, thanks! - quaresma89
  • By the way, your option does not quite suit me since after selecting a page is overloaded, therefore the element in the array is only from the last select! - quaresma89
  • @ quaresma89 element is not from the last select, but from the selected select. - Jean-Claude