如何获取jquery anchor href值

时间:2022-11-27 17:29:13

jQuery:

$(document).ready(function() {
    $("a.change_status").click(function(){
       var status_id = $("a").val();
       alert(status_id); 
       return false;
    });
});

HTML:

<a href="?status=5" class="change_status">Aminul</a><br/>
<a href="?status=25" class="change_status">Arif</a><br/>
<a href="?status=15" class="change_status">Sharif</a><br/>

I need status_id and for some reason my anchor tag is dynamic. I can't use id or make class name dynamic. I think, I need to use $this to get my value.

我需要status_id,由于某种原因我的锚标签是动态的。我不能使用id或make class name dynamic。我想,我需要使用$ this来获取我的价值。

7 个解决方案

#1


12  

This one is simple :

这个很简单:

     $(document).ready(function() {
         $("a.change_status").click(function(){
           var status_id = $(this).attr('href').split('=');
           alert(status_id[1]); 
           return false;
        });
    });

http://jsfiddle.net/NmzRV/

#2


5  

var status_id= $(this).attr("href").match(/status=([0-9]+)/)[1];

#3


3  

You have two separate issues here... first you need to find the actual clicked link using this and then find the value of that href attribute.

这里有两个不同的问题...首先你需要找到实际点击的链接,然后找到该href属性的值。

$(document).ready(function() {
    $("a.change_status").click(function() {
        var status_id = parseURL($(this).attr("href"));
        alert(status_id);
        return false;
    });
});

Also, because javascript doesn't have a way to pull URL parameters, you must write a function (in the example parseURL) in which to find the value of the variable "status":

此外,由于javascript无法提取URL参数,因此必须编写一个函数(在示例parseURL中),在该函数中查找变量“status”的值:

function parseURL(theLink) {
    return decodeURI((RegExp('status=' + '(.+?)(&|$)').exec(theLink) || [, null])[1]);
}

See the following jsfiddle:

请参阅以下jsfiddle:

http://jsfiddle.net/ZKMwU/

#4


2  

$('a').attr('href');

should do the trick

应该做的伎俩

#5


1  

$(document).ready(function() {
        $("a.change_status").click(function(){
           var status_id = $(this).attr("href");
           alert(status_id); return false;
        });
    });

#6


1  

You can do this like :

你可以这样做:

$(document).ready(function() { 
  $("a.change_status").click(function() { 
      var status_id = $(this).attr("href"); 
      alert(status_id);
      return false;
  }); 
});

#7


0  

$("a.change_status").click(function(){ 
     var status_id = $(this).attr('href'); 
     alert(status_id); 
});

#1


12  

This one is simple :

这个很简单:

     $(document).ready(function() {
         $("a.change_status").click(function(){
           var status_id = $(this).attr('href').split('=');
           alert(status_id[1]); 
           return false;
        });
    });

http://jsfiddle.net/NmzRV/

#2


5  

var status_id= $(this).attr("href").match(/status=([0-9]+)/)[1];

#3


3  

You have two separate issues here... first you need to find the actual clicked link using this and then find the value of that href attribute.

这里有两个不同的问题...首先你需要找到实际点击的链接,然后找到该href属性的值。

$(document).ready(function() {
    $("a.change_status").click(function() {
        var status_id = parseURL($(this).attr("href"));
        alert(status_id);
        return false;
    });
});

Also, because javascript doesn't have a way to pull URL parameters, you must write a function (in the example parseURL) in which to find the value of the variable "status":

此外,由于javascript无法提取URL参数,因此必须编写一个函数(在示例parseURL中),在该函数中查找变量“status”的值:

function parseURL(theLink) {
    return decodeURI((RegExp('status=' + '(.+?)(&|$)').exec(theLink) || [, null])[1]);
}

See the following jsfiddle:

请参阅以下jsfiddle:

http://jsfiddle.net/ZKMwU/

#4


2  

$('a').attr('href');

should do the trick

应该做的伎俩

#5


1  

$(document).ready(function() {
        $("a.change_status").click(function(){
           var status_id = $(this).attr("href");
           alert(status_id); return false;
        });
    });

#6


1  

You can do this like :

你可以这样做:

$(document).ready(function() { 
  $("a.change_status").click(function() { 
      var status_id = $(this).attr("href"); 
      alert(status_id);
      return false;
  }); 
});

#7


0  

$("a.change_status").click(function(){ 
     var status_id = $(this).attr('href'); 
     alert(status_id); 
});