如何在一个中发布到href的php值

时间:2013-04-24 12:53:42

标签: jquery ajax post

我已经能够使用课程<a href="">检索所有various

我需要编写一些脚本,从这些href中抓取a(每个人都有不同)并将其发布到popup.php。

棘手的部分是href有gallery_popup.php??id=XX,我唯一需要发布的是ID。

我决定使用变量,所以首先:

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

我抓住了href。然后我想撕掉所有不必要的东西,只留下身份证号码并保存到Var = IDnumber

然后

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

4 个答案:

答案 0 :(得分:1)

或者你可以做

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

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

答案 1 :(得分: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);
});

答案 2 :(得分:0)

您可以在“rel”中保存身份证号码,

  <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]);

答案 3 :(得分:0)

只需一行:

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