admin管理员组文章数量:1323723
I am using a Javascript function to get the values of a URL to pass to jQuery using the function below:
function getUrlVars()
{
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
}
And then setting the value like this:
var type = getUrlVars()["type"]
This all works perfectly, however I have just e into a situation where I need to get multiple values, one of my form elements are checkboxes where multiple values can be checked, so my URL will look something like this:
.php?type=1&cuisine[]=23&cuisine[]=43&name=test
If I alert out the cuisine value using the function above I only ever get the final value:
alert (getUrlVars()["cuisine[]"]);
Would alert "43".
What I would like it to be is a ma delimited string of all "cuisine" values. ie in the above example "23,43"
Any help very wele! In case any solution requires it I am using PHP 5.3 and Jquery 1.4
I am using a Javascript function to get the values of a URL to pass to jQuery using the function below:
function getUrlVars()
{
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
}
And then setting the value like this:
var type = getUrlVars()["type"]
This all works perfectly, however I have just e into a situation where I need to get multiple values, one of my form elements are checkboxes where multiple values can be checked, so my URL will look something like this:
http://www.domain./test.php?type=1&cuisine[]=23&cuisine[]=43&name=test
If I alert out the cuisine value using the function above I only ever get the final value:
alert (getUrlVars()["cuisine[]"]);
Would alert "43".
What I would like it to be is a ma delimited string of all "cuisine" values. ie in the above example "23,43"
Any help very wele! In case any solution requires it I am using PHP 5.3 and Jquery 1.4
Share Improve this question asked May 17, 2010 at 13:32 bateman_apbateman_ap 1,9217 gold badges28 silver badges41 bronze badges4 Answers
Reset to default 2 function getUrlVars()
{
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
if($.inArray(hash[0], vars)>-1)
{
vars[hash[0]]+=","+hash[1];
}
else
{
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
}
return vars;
}
You just have to check in you for loop if vars[hash[0]] already exists and if so then instead of doing vars[hash[0]] = hash[1]; you'll do vars[hash[0]] += ',' + hash[1];
A more proper solution, as the query string should have unique keys is to add a hidden field to the form, and then in the onsubmit
event you loop over the checkboxes and set the value of the hidden field to the aggregated values form the checkboxes.
The script
function getValues(){
var inputs= document.getElementById("myForm").getElementsByTagName("input"), i=inputs.length;
var values=[];
while (i--){
if (inputs[i].type == "checkbox" && inputs[i].checked) {
values.push(inputs[i].value);
}
}
document.getElementById("hiddenField").value=values.join();
}
And the HTML
<form id="myForm" onsubmit="getValues()">
<input type="hidden" name="values" id="hiddenField"/>
<input type="checkbox" id="chk1" value="1"/>
<input type="checkbox" id="chk1" value="2"/>
<input type="submit" value="submit"/>
</form>
This is better than overriding the default (and expected behavior) of key/value based parameters.
Try this plugin
本文标签:
版权声明:本文标题:jquery - Using Javascript to get URL Vars, not working when multiple values present in QueryString - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742123080a2421813.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论