often I find myself suprized about simple th开发者_开发百科ings, I'm not sure but I believe that this might be one of them.
Often I find myself needing to merge get attributes, especially within filters and search pages within sites. This come be at times quite complicated, as you might want to remove and add parameters dynamically.
Simple way to do it is to use a form, but this get complicated, when adding elements that don't belong within a form. e.g. pagination
For example I might have a URL such as the following
http://www.example.org/ninja_search/?speed__gt=5&night_sight=1&page=1
How would I merge such URL with page=2, substituting the 1 with the new value? Is javascript the only option?
?speed__gt=5&night_sight=1&page=1 + page=2 = ?speed__gt=5&night_sight=1&page=2
Thanks :)
The solution is to use []
to explicitly create an array from the parameters, providing your server side language supports it:
?speed__gt=5&night_sight=1&page[]=1&page[]=2
For example, if your server-side language is PHP, $_GET['page']
will now return an array with 1 as the first element and 2 as the second.
You can use this technique in forms too, by applying it to the name
attribute:
<form id="myform">
<input type="hidden" name="page[]" value="1" />
<input type="hidden" name="page[]" value="2" />
</form>
If you're talking about adding a second parameter and ignoring the first, this will happen anyway - the second provided parameter will override the first. Assuming
?speed__gt=5&night_sight=1&page=1&page=2
The value of page
at the server will be 2, because the last value given takes priority.
精彩评论