i have url http://localhost/testo.php?id1=20&qty1=5&id2=13&qty2=4&id3=23&qty3=5.5 and so on with id4 etc etc
并成功获取带有以下代码的参数:
var a = location.href;
var b = a.substring(a.indexOf("?")+1);
var b的结果值是:
id1=20&qty1=5&id2=13&qty2=4&id3=23&qty3=5.5
我想将var b的结果值以及1,2和3的索引解析为javascript整数/十进制变量,所以结果如下:
id1 = 20
qty1 = 5
id2 = 13
qty2 = 4
id3 = 23
qty3 = 5.5
最重要的是如何获得1,2和3的索引
请帮忙......>。<
非常感谢:)
答案 0 :(得分:0)
您的网址有点不标准:http://localhost/testo.php?id1=20?qty1=5?id2=13?qty2=4?id3=23?qty3=5.5
因此,您的网址应为http://localhost/testo.php?id1=20&qty1=5&id2=13&qty2=4&id3=23&qty3=5.5
要解析您的GET参数: How can I get query string values in JavaScript?
答案 1 :(得分:0)
您将要使用getParameterByName()
。您进行设置的方式看起来不错:
getParameterByName('id1');
#20
getParameterByName('qty1');
#5
getParameterByName('id2');
#13
getParameterByName('qty2');
#4
getParameterByName('id3');
#23
getParameterByName('qty3');
#5.5
答案 2 :(得分:0)
var url = 'http://localhost/testo.php?id1=20?qty1=5?id2=13?qty2=4?id3=23?qty3=5.5';
var result = [];
url.split('?').forEach(function(str, index) {
if(index === 0) {
return;
}
var arr = str.split('=');
var nameParts;
if(arr.length > 1) {
nameParts = /^([^\d]+)(\d+)$/.exec(arr[0]);
if(nameParts && nameParts.length > 2) {
result.push({
key: nameParts[1],
index: nameParts[2],
value: arr[1]
});
}
}
});
console.dir(result);
/*
output:
0 Object { key="id", index="1", value="20"}
1 Object { key="qty", index="1", value="5"}
2 Object { key="id", index="2", value="13"}
3 Object { key="qty", index="2", value="4"}
4 Object { key="id", index="3", value="23"}
5 Object { key="qty", index="3", value="5.5"}
*/