Get parameter values from href in jquery

前端 未结 2 1887
温柔的废话
温柔的废话 2020-12-06 11:39

Script

 

        
相关标签:
2条回答
  • 2020-12-06 12:24

    Try this :

    function getUrlParams(url) {
        var params = {};
        url.substring(1).replace(/[?&]+([^=&]+)=([^&]*)/gi,
                function (str, key, value) {
                     params[key] = value;
                });
        return params;
    }
    

    Usage:

    function pop(e) {
         var url = $(e.target).attr("href");
         var params = getUrlParams(url); 
    
         var cat= params["cat"];
         var type= params["typ"];
    
         alert(cat);
         alert(type);    
    }
    

    Working Fiddle

    0 讨论(0)
  • 2020-12-06 12:25

    You can do as soeme thing as below

    $('a').bind('click',function(){
        var url = ($(this).attr('href'));
        var cat = getURLParameter(url, 'cat');
        var typ = getURLParameter(url, 'typ');
        //calling the ajax function
        pop(cat, typ)
    });
    
    
    function getURLParameter(url, name) {
        return (RegExp(name + '=' + '(.+?)(&|$)').exec(url)||[,null])[1];
    }
    
    function pop(cat, typ) {
    
        $.ajax({
           type:'post',
           url:site_url()+'/event/deleteEventSingle',
           data:{'cat':cat,'type':typ},
            async:false,
             success:function(result){}
        });
    }
    

    Check out the the example at Live fiddle http://jsfiddle.net/mayooresan/aY9vy/

    0 讨论(0)
提交回复
热议问题