-2

I have some problems in jquery

I have a problem which is how I can get out some numbers of links

I just want from the table

id=1293399
id=609876
id=6789234
id=3421

Of the following links

href="/inedx.php?form=11732&id=1293399&min=info_custom
href="/inedx.php?form=11732&id=609876&min=info_custom
href="/inedx.php?form=11732&id=6789234&min=info_custom
href="/inedx.php?form=11732&id=3421&min=info_custom

Experimented with this, But I could not get them properly

<script>
    $(function(){
        $("#ABC tr").each(function(){
        var A = $(this).find('td:nth-child(1)').find('a').attr("href");
        var B = $(A).match(/[\d]+/d);
        alert(B);
        });
    });
</script>




<body> 
<table id='ABC' border='2'>
<tr>
<td><a href='href="/inedx.php?form=11732&id=1293399&min=info_custom'>AAAy</a></td>
<td>BBB</td>
<td>CCC</td>
</tr>
<tr>
<td><a href='href="/inedx.php?form=11732&id=609876&min=info_custom'>AAAy</a></td>
<td>BBB</td>
<td>CCC</td>
</tr>
<tr>
<td><a href='href="/inedx.php?form=11732&id=6789234&min=info_custom'>AAAy</a></td>
<td>BBB</td>
<td>CCC</td>
</tr>
<tr>
<td><a href='href="/inedx.php?form=11732&id=3421&min=info_custom'>AAAy</a></td>
<td>BBB</td>
<td>CCC</td>
</tr>
</table>
</body> 

How do I get this only

id=1293399
id=609876
id=6789234
id=3421
4

5 に答える 5

2
var A = "...",
    link = A,
    num = link.match(/id=(\d+)/);
num[0];  //"id="+number
num[1];  //only number

var B = num[0];
//Hey I just met you,
//and this is crazy,
//But here's your B,
//so use it maybe. :D

MDN: String.match

Live demo: http://jsfiddle.net/zBspF/

于 2013-01-02T21:58:54.557 に答える
1

Here's another answer that doesnt depend on the position id param :

$(function() {
    $("#ABC tr").each(function() {
        var url = $(this).find('td:nth-child(1)').find('a').attr("href");
        alert("id="+getURLParam("id", url));
    });
});

function getURLParam(name, url) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(url);
    if (results == null) return "";
    else return results[1];
}​

Fiddle: http://jsfiddle.net/44rFm/1/

note: logic borrowed from implementation of gup() function given in http://www.netlobo.com/url_query_string_javascript.html

于 2013-01-03T14:17:12.530 に答える
0
$(function() {
    $('a').each(function() {
    var href = $(this).attr('href');
  /*var id = href.substring(href.indexOf('&') + 1, href.lastIndexOf('&'));*/
    var id = href.match(/id=\d+/g);
    $('body').append(id).append('<br>') //append to body
   //alert(id)
   })
});  

jsfiddle

于 2013-01-02T22:07:17.927 に答える
0

Here's a slightly hacky approach

var B= "id="+A.split("&id=")[1].split("&")[0];

js fiddle: http://jsfiddle.net/uGrPT/

于 2013-01-02T22:00:29.303 に答える
0

attr() returns the attribute value, so A is the href string. $(A) would then look for a node using the href string as a jquery selector -- surely that's not what you want to do.

I would change the regexp match line to:

var B = A.match(/id=\d+/);
于 2013-01-02T22:54:16.707 に答える