[英]How to get multiple parameters with same name from a URL in JavaScript
[英]Javascript: Get last result from multiple vars with same name
在尝试修复以前的程序员的错误时,我发现了一个if函数,该函数提供相同的变量多个信息,从而使我们的链接混乱。
// This piece of script, when there is multiple 'x' selections: if (x == 0){ var searchBrand = "" } else if (x == 1) { var searchBrand = "one" } else if (x == 2) { var searchBrand = "two" } else if (x == 3) { var searchBrand = "three" } else if (x == 4) { var searchBrand = "four" } else if (x == 5) { var searchBrand = "five" } // and processed via: var newurl = $(this).attr('href').replace('/search?q=', '/search?q=' + searchBrand + '%20'+ '%20'+ '%20'); // output in reverse order selected: /search?q=four%20%20%20two%20%20%20five%20%20%20
我尝试过类似的方法:
else if (x == 3) { var sB = { searchBrand: three; } } var newurl = $(this).attr('href').replace('/search?q=', '/search?q=' + sB.searchBrand + '%20')
和别的。
我要实现的是仅在newurl变量中获取最后选择的x的searchBrand值。
如果是ladder,则将searchBrand变量声明为其他变量。
var searchBrand = "";
// This piece of script, when there is multiple 'x' selections:
if (x == 0){
searchBrand = "";
}
else if (x == 1) {
searchBrand = "one";
}
else if (x == 2) {
searchBrand = "two";
}
else if (x == 3) {
searchBrand = "three";
}
else if (x == 4) {
searchBrand = "four";
}
else if (x == 5) {
searchBrand = "five";
}
在下面使用这样的数组
let searchBrand = ['','one','two','three','four','five']
var newurl = $(this).attr('href').replace('/search?q=', '/search?q=' + searchBrand[x] + '%20'+ '%20'+ '%20');
let queryParameter = ''
while(numberOfIterations){
queryParameter = queryParameter+searchBrand[x]+'%20%20%20'
} //numberOfIterations is the number of times the value x is input
另外,我认为这个问题的框架并不完善。 那么,您可以更好地重新构架吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.