Javascript 获取数据键值并包含在URL before.html中

Javascript 获取数据键值并包含在URL before.html中,javascript,Javascript,下面的代码用于从数据键获取值并与.html连接 初始Url将为- 根据键值(Ex:25888)收集需要在url内追加如下内容 但现在,若用户选择下一个选项值,它将获得最新的URL并包含新的密钥。在每个choose值上,它都包含到URL 像- 但是,它必须是唯一的 $('a.dropdown-item')。在('click',function()上{ 让getDataKey=$(this.attr('data-key'); 让getWindowLocation=window.location.

下面的代码用于从数据键获取值并与.html连接

初始Url将为-

根据键值(Ex:25888)收集需要在url内追加如下内容

但现在,若用户选择下一个选项值,它将获得最新的URL并包含新的密钥。在每个choose值上,它都包含到URL

像-

但是,它必须是唯一的

$('a.dropdown-item')。在('click',function()上{
让getDataKey=$(this.attr('data-key');
让getWindowLocation=window.location.href;
让getLocationVal=getWindowLocation.replace(/\.html/,'.+getDataKey+'.html');
window.location.replace(getLocationVal);

});您还可以在正则表达式中包含可选的数字匹配器
[.0-9]

[0-9]
字符范围0-9
*
前面的字符为零或多个

代码示例:

$('a.dropdown-item').on('click', function(){
    let getDataKey = $(this).attr('data-key');
    let getWindowLocation = window.location.href;
    let getLocationVal = getWindowLocation.replace(/[.0-9]*\.html/, '.' + getDataKey + '.html');
    window.location.replace(getLocationVal); 
});
演示:

$('a.dropdown-item').on('click', function(){
    let getDataKey = $(this).attr('data-key');
    let getWindowLocation = window.location.href;
    let getLocationVal = getWindowLocation.replace(/[.0-9]*\.html/, '.' + getDataKey + '.html');
    window.location.replace(getLocationVal); 
});
让string1=”http://www.test.com/en/location/london.html";
让string2=”http://www.test.com/en/location/london.3899091.html";
log(“String1:+String1.replace(/[.0-9]*\.html/,”.+“454545.html”);
log(“String2:+String2.replace(/[.0-9]*\.html/,”.+“25888.html”)