gpt4 book ai didi

javascript - 提取链接中 ?= 后的字符

转载 作者:行者123 更新时间:2023-12-02 19:01:32 24 4
gpt4 key购买 nike

任何人都可以看到我在这里出错的地方,我试图获取链接中 ?p= 之后的最后一个字符并放入变量中,但我使用的代码返回/。

我的代码是:

 $("#dvPartial").on('click', '.dvPagerCities a', function (event) {
alert('click detected');
var city = ($('input#hdnCountry').val());
alert(city);
var link = $('a').attr('href');
//var getEqualPosition = link.indexOf('?p='); //Get the position of '='
var getEqualPosition = link.indexOf('='); //Get the position of '='
var number = link.substring(getEqualPosition + 1); //Split the string and get the number.

我的链接是

<a href="/Weather/Index/Australia?p=2">»</a>

我认为正在发生的事情是它拿起了第一个 = 。

我的理论是这样的。

1)检测点击事件

2) 获取引发事件的链接

3)提取p的值,

p 可以是 1 个数字、2 个数字或 3 个数字。

任何帮助将不胜感激,因为似乎我一解决一个问题就会出现另一个问题。

谢谢

乔治

最佳答案

假设 p 是 URL 中唯一的参数,您可以简单地用 = 符号分割:

$("#dvPartial").on('click', '.dvPagerCities a', function (event) {
var city = $('input#hdnCountry').val();
var number = $(this).attr('href').split('=')[1];
});

关于javascript - 提取链接中 ?= 后的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14749476/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com