jQuery设置url字符串中的变量
我有一个URL字符串,如 我希望能够在每个?人物 我有它的工作得到最后的价值-但似乎不能存储第一 用于获取最后一个?值的代码为:jQuery设置url字符串中的变量,jquery,Jquery,我有一个URL字符串,如 我希望能够在每个?人物 我有它的工作得到最后的价值-但似乎不能存储第一 用于获取最后一个?值的代码为: window.lastValue = window.location.href.substring(window.location.href.lastIndexOf('?') + 1); 我怎样才能存储第一个呢 所以firstValue=鸡肉,lastValue=吐司 更新-如何通过展平数组来存储totalVal?因此,如果数组是[“chicken”,“toast”
window.lastValue = window.location.href.substring(window.location.href.lastIndexOf('?') + 1);
我怎样才能存储第一个呢
所以firstValue=鸡肉,lastValue=吐司
更新-如何通过展平数组来存储totalVal?因此,如果数组是[“chicken”,“toast”],我需要将其平铺成一个字符串,在数组中的每个项目之前都有一个“.”。因此,如果数组是[“chicken”,“toast”],它将变成“.chicken.toast”-如果数组是[“chicken”],它将变成“.chicken”//谢谢
干杯,这将为您提供所需的一系列值:
var r = window.location.href.split("?");
r.shift();
console.log(r);
如果始终正好有两个值,则可以使用此选项提取它们:
var val1 = r.shift();
var val2 = r.shift();
这里有一个版本给出了.chicken
结果:
var r = window.location.href.split("?");
r[0]='';
var totalval = r.join('.');
这将为您提供所需值的数组:
var r = window.location.href.split("?");
r.shift();
console.log(r);
如果始终正好有两个值,则可以使用此选项提取它们:
var val1 = r.shift();
var val2 = r.shift();
这里有一个版本给出了.chicken
结果:
var r = window.location.href.split("?");
r[0]='';
var totalval = r.join('.');
我建议:
// obviously, in production you should use document.location.href:
var url = "http://www.example.com/?chicken?toast",
// take a substring of the url variable, starting at the index of
// the (first) '?' character and running to the end of the string,
// giving "?chicken?toast", we then split that resultant string
// on the '?' characters, and filter the resulting array using
// filter(Boolean), which retains only the true/truthy array-elements:
values = url.substring(url.indexOf('?')).split('?').filter(Boolean);
console.log(values);
var url=”http://www.example.com/?chicken?toast",
values=url.substring(url.indexOf('?')).split('?').filter(布尔);
console.log(值)代码>我建议:
// obviously, in production you should use document.location.href:
var url = "http://www.example.com/?chicken?toast",
// take a substring of the url variable, starting at the index of
// the (first) '?' character and running to the end of the string,
// giving "?chicken?toast", we then split that resultant string
// on the '?' characters, and filter the resulting array using
// filter(Boolean), which retains only the true/truthy array-elements:
values = url.substring(url.indexOf('?')).split('?').filter(Boolean);
console.log(values);
var url=”http://www.example.com/?chicken?toast",
values=url.substring(url.indexOf('?')).split('?').filter(布尔);
console.log(值)代码>var sPageURL=decodeURIComponent(window.location.search.substring(1)),
sURLVariables=sPageURL.split(“&”),
sParameterName,
我
对于(i=0;i
此参考可能对您有所帮助:
var sPageURL=decodeURIComponent(window.location.search.substring(1)),
sURLVariables=sPageURL.split(“&”),
sParameterName,
我
对于(i=0;i
此参考可能对您有所帮助:
好的,谢谢-然后需要将数组展平为2个变量yeh?您可以根据需要提取数组的值。我已经编辑了一个方法。最后一个问题,如果可以的话。。。我是否可以将数组展平为1个变量,值以“.”分隔-如果字符串中有2个参数,则为:var totalval=“.chicken.toast”,而var totalval=“.chicken”如果只有一个?我已经添加了一个进一步的编辑,以给出.chicken.toast
输出Hanks Chris-我假设我可以运行脚本,这样我就可以提取数组结果的关联列表-例如“.chicken.toast”以及单个变量-例如“chicken”和“toast”?好的,谢谢-然后需要将数组展平为2个变量yeh?您可以根据需要提取数组的值。我已经编辑了一个方法。最后一个问题,如果可以的话。。。我是否可以将数组展平为1个变量,值以“.”分隔-如果字符串中有2个参数,则为:var totalval=“.chicken.toast”,而var totalval=“.chicken”如果只有一个?我已经添加了一个进一步的编辑,以给出.chicken.toast
输出Hanks Chris-我假设我可以运行脚本,这样我就可以提取数组结果的关联列表-例如“.chicken.toast”以及单个变量-例如“chicken”和“toast”?谢谢-这将它们放入一个数组yeh-因此需要将它们从数组解析为2个变量yeh?或者简单地将它们作为值[0]
,值[1]
:它们已经在一个数组中,您可以使用数组[index]符号访问它们。谢谢。大家都明白了。谢谢-这会将它们放入数组yeh-因此需要将它们从数组解析为2个变量yeh?或者简单地将它们称为值[0]
,值[1]
:它们已经在数组中,您可以使用数组[index]符号访问它们。谢谢。大家都明白了。