2017-10-20 89 views
0

我想從URL獲取$_GET["id"]

$(document).ready(function(){ 
    //TRAITEMENT ENVOIE INVITATION 
    $("#amis_commun_liste .afficher_plus_modal").bind('click', function f() { 
     var afficher_plus_modal = $(this).attr("class"); 
     $(this).unbind('click', f); 
     $.ajax({ 
      type: "post", 
      url: "voir_profil_includes/func_infos.php", 
      data: { 
       "afficher_plus_modal": afficher_plus_modal, 
       //I want to get here $_GET['id']   
      }, 
      beforeSend: function() { 
       $("#amis_commun_liste .afficher_plus_modal").html("En cours"); 
      }, 
      success: function(data) { 
       if (data != "success") {       
        alert(data); 
       } 
      } 
     }); 
    }); 
}); 

是否有可能從URL使用jQuery獲得$_GET[]

+0

從哪裏得到這個'id' GET變量? – void

+0

你爲什麼不簡單地解析url?或從URL獲取參數值? –

+0

來自當前頁面的URL – kam

回答

0

您可以使用此功能

function getParameterByName(name, url) { 
    if (!url) url = window.location.href; 
    name = name.replace(/[\[\]]/g, "\\$&"); 
    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"), 
     results = regex.exec(url); 
    if (!results) return null; 
    if (!results[2]) return ''; 
    return decodeURIComponent(results[2].replace(/\+/g, " ")); 
} 

使用

// query string: ?foo=lorem&bar=&baz 
var foo = getParameterByName('foo'); // "lorem" 

在您的代碼:

data: { 
    "afficher_plus_modal": afficher_plus_modal, 
    "id": getParameterByName('id')   
}, 

只是不要忘記定義函數beforhand。