how to get jquery anchor href value

大憨熊 提交于 2019-12-05 01:26:42

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/

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

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.

$(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":

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

See the following jsfiddle:

http://jsfiddle.net/ZKMwU/

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

should do the trick

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

You can do this like :

$(document).ready(function() { 
  $("a.change_status").click(function() { 
      var status_id = $(this).attr("href"); 
      alert(status_id);
      return false;
  }); 
});
$("a.change_status").click(function(){ 
     var status_id = $(this).attr('href'); 
     alert(status_id); 
});
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!