点击按钮链接- jquery

时间:2022-11-21 15:41:37

I have a script as below

我有如下脚本

$('.button1').click(function() {
    document.location.href=$(this).attr('id');
});

the button1 has variable unique ids. on click, the page must redirect to url "www.example.com/index.php?id=buttonid" but now the page is redirecting only to "button id".

button1有变量唯一id。在单击时,页面必须重定向到url www.example.com/index.php?id=buttonid"但现在页面只重定向到"button id"。

I want to add the string "www.example.com/index.php?id=" before the current url. How can I make this possible?

查查查询www.example.com/index.php?id="在当前url之前。我怎样才能使这成为可能?

5 个解决方案

#1


56  

$('.button1').click(function() {
   window.location = "www.example.com/index.php?id=" + this.id;
});

First of all using window.location is better as according to specification document.location value was read-only and might cause you headaches in older/different browsers. Check notes @MDC DOM document.location page

首先使用窗口。根据规范文件,位置更好。位置值是只读的,可能会导致老/不同的浏览器头疼。检查notes @MDC DOM文档。页面位置

And for the second - using attr jQuery method to get id is a bad practice - you should use direct native DOM accessor this.id as the value assigned to this is normal DOM element.

对于第二个使用attr jQuery方法来获取id是一个糟糕的实践——您应该使用直接的本地DOM访问器。id作为赋给它的值是普通的DOM元素。

#2


5  

$('.button1').click(function() {
   document.location.href='/index.php?id=' + $(this).attr('id');
});

#3


4  

You need to specify the domain:

您需要指定域名:

 $('.button1').click(function() {
   window.location = 'www.example.com/index.php?id=' + this.id;
 });

#4


2  

Why not just change the second line to

为什么不把第二行改为

document.location.href="www.example.com/index.php?id=" + $(this).attr('id');

#5


0  

you can get the current url with window.location.href but I think you will need the jQuery query plugin to manipulate the query string: http://plugins.jquery.com/project/query-object

可以使用windows .location获得当前url。href但是我认为您需要jQuery查询插件来操作查询字符串:http://plugins.jquery.com/project/queryobject。

#1


56  

$('.button1').click(function() {
   window.location = "www.example.com/index.php?id=" + this.id;
});

First of all using window.location is better as according to specification document.location value was read-only and might cause you headaches in older/different browsers. Check notes @MDC DOM document.location page

首先使用窗口。根据规范文件,位置更好。位置值是只读的,可能会导致老/不同的浏览器头疼。检查notes @MDC DOM文档。页面位置

And for the second - using attr jQuery method to get id is a bad practice - you should use direct native DOM accessor this.id as the value assigned to this is normal DOM element.

对于第二个使用attr jQuery方法来获取id是一个糟糕的实践——您应该使用直接的本地DOM访问器。id作为赋给它的值是普通的DOM元素。

#2


5  

$('.button1').click(function() {
   document.location.href='/index.php?id=' + $(this).attr('id');
});

#3


4  

You need to specify the domain:

您需要指定域名:

 $('.button1').click(function() {
   window.location = 'www.example.com/index.php?id=' + this.id;
 });

#4


2  

Why not just change the second line to

为什么不把第二行改为

document.location.href="www.example.com/index.php?id=" + $(this).attr('id');

#5


0  

you can get the current url with window.location.href but I think you will need the jQuery query plugin to manipulate the query string: http://plugins.jquery.com/project/query-object

可以使用windows .location获得当前url。href但是我认为您需要jQuery查询插件来操作查询字符串:http://plugins.jquery.com/project/queryobject。