简体   繁体   English

jQuery-使用选择器查找href

[英]jquery - using selector to find an href

Background Information: 背景资料:

I'm trying to find the value in the attribute "row_id" in an href. 我正在尝试在href的属性“ row_id”中查找值。 What will trigger this logic is when the user clicks on a "Save changes" button / image. 当用户单击“保存更改”按钮/图像时,将触发此逻辑。

Problem Description 问题描述

The jquery code I have right now is returning the contents of the second cell instead of the one with the href in it. 我现在拥有的jQuery代码将返回第二个单元格的内容,而不是其中包含href的那个。 So from the HTML sample code below, I need to extract 21586 from 因此,从下面的HTML示例代码中,我需要从中提取21586

 "<a href="index.php?page=row&amp;row_id=21586">Row SAA:HH1</a>"

HTML Code HTML代码

The button that triggers the jquery logic is in the 4th cell of the row, and the row_id is in the href in the 5th cell. 触发jquery逻辑的按钮在该行的第4个单元格中,并且row_id在第5个单元格的href中。

 <tr>
    <td id="21581">
        <img src="?module=chrome&amp;uri=pix/tango-user-trash-16x16-gray.png" title="1 rack(s) here" height="16" width="16" border="0">
    </td>
    <td><div id="location_name">SAA:HH-123</div></td>
    <td><div id="row_name">SAA:HH1</div></td>
    <td>
         <input tabindex="1" name="edit" class="edit" src="?module=chrome&amp;uri=pix/pencil-icon.png" id="21581" title="Edit row" type="image" border="0">&nbsp;
         <input tabindex="1" style="display: none;" name="submit" class="icon" src="?module=chrome&amp;uri=pix/tango-document-save-16x16.png" title="Save changes" type="image" border="0">
   </td>
   <td>
        <a href="index.php?page=row&amp;row_id=21586">Row SAA:HH1</a>
   </td>
 </tr>

Jquery Code jQuery代码

    //save button handler
    $(".icon").click(function(e){
            //find the <a href that has the row id embbeded in it
            var row_id = $(this).parent().siblings().children($('a[href$="row_id"]'));
            console.log($(row_id).text());
    });

Any suggestions would be appreciated. 任何建议,将不胜感激。

You could try this: 您可以尝试以下方法:

http://jsfiddle.net/vsLy0rfx/ http://jsfiddle.net/vsLy0rfx/

$(".icon").click(function(e){
        //find the <a href that has the row id embbeded in it
        var row_link = $(this).parent().siblings().find('a[href*="row_id"]')
        var row_id = row_link.attr('href').split('row_id=')[1];
        console.log(row_id);
});

check the console, it returns 21586. i used .closest() and .find() to navigate and find the <a> in the corresponding row. 检查控制台,它返回21586。我使用.closest().find()进行导航并在对应的行中找到<a> then i got the href attribute and split it to find the desired id 然后我得到了href属性并将其拆分以找到所需的id

 $(document).ready(function() { $('.icon').on('click', function() { var href = $(this).closest('tr').find('a').attr('href'); var id = href.split('&')[1].split('=')[1]; console.log(id); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr> <td id="21581"> <img src="?module=chrome&amp;uri=pix/tango-user-trash-16x16-gray.png" title="1 rack(s) here" height="16" width="16" border="0"/> </td> <td><div id="location_name">SAA:HH-123</div></td> <td><div id="row_name">SAA:HH1</div></td> <td> <input tabindex="1" name="edit" class="edit" src="?module=chrome&amp;uri=pix/pencil-icon.png" id="21581" title="Edit row" type="image" border="0"/>&nbsp; <input tabindex="1" style="" name="submit" class="icon" src="?module=chrome&amp;uri=pix/tango-document-save-16x16.png" title="Save changes" type="image" border="0"/> </td> <td> <a href="index.php?page=row&amp;row_id=21586">Row SAA:HH1</a> </td> </tr> </table> 

Try using following 尝试使用以下

var result = $('a[href]',$(this).parent().parent()).attr('href').split("=").pop();

//result will be equal to 21586 //结果将等于21586

Try this . 尝试这个 。 Could be used universally for any url and parameter value. 可以普遍用于任何url和参数值。

$.getUrlParamValue = function(link, parameter){
    var results = new RegExp('[\?&]' + parameter + '=([^&#]*)').exec(link);
    return results[1] || 0;
}

var value  = $.getUrlParamValue ($("a").attr("href"),'row_id');
alert(value);

http://jsfiddle.net/r2xsse9v/ http://jsfiddle.net/r2xsse9v/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM