2013-04-24 48 views
2

我已經能夠檢索所有<a href="">various如何發佈到php值的href在

我需要編寫一些腳本,從這些a(每個人都有不同)抓取href並將其發佈到popup.php。

棘手的部分是,href有gallery_popup.php??id=XX,我唯一需要發佈的就是該ID。

我決定去與變量,因此第一:

var href = $('.various').attr('href'); 

我搶在href。然後,我竟被喜歡抓取所有不必要的東西,只留下ID號,並將其保存到無功=的ID號

然後

$(".various").click(function() { 
    $.post('gallery_popup.php', IDNumber; 
}); 
+0

你的ID是如何分配的?你可以給他們添加一個'rel'並抓住它。 ' ckpepper02 2013-04-24 13:00:16

+0

[JavaScript的查詢字符串](http://stackoverflow.com/a/647272/1244588 )只需使用你的'href'而不是location.search – dualed 2013-04-24 13:02:49

回答

0

您可以使用這樣的事情:

$(".various").click(function() { 
    var url = $(this).attr('href')), 
     matches = url.match(/gallery_popup\.php\?\?id=(\d+)/), 
     id; 

    // If an id has been found, matches will contain 2 elements : 
    // 1 - the whole chain matched (here "gallery_popup.php??id=XX") 
    // 2 - the group captured (here XX) 
    if(matches.length > 1) id = matches[1]; 

    $.post('gallery_popup.php', id); 
}); 
1

,或者你可以做

var myString = $(this).attr('href'); 
var myArray = myString.split('='); 

$.post('gallery_popup.php', myArray[myArray .length-1]); 
0

您可以保存在「相對」像身份證號碼,

<a href="gallery_popup.php??id=XX" rel="xx" class="various" >gallery</a> 

然後你就可以訪問ID,

var href = $('.various').attr('rel'); 

如果你想使用

var href = $('.various').attr('href'); 

你可以分割你的網址 'ID ='

eg: var temp = href.split("id="); 
     alert(temp[1]); 
0

只要做到這一點在一行中:

$.post('gallery_popup.php', $(this).attr("href").split("=")[1]);