2015-10-20 59 views
0

背景資料:jQuery的 - 用選擇找一個href

我試圖找到在一個href屬性「ROW_ID」的值。當用戶點擊「保存更改」按鈕/圖像時,會觸發該邏輯的是什麼。

問題描述

jQuery代碼我已現在正在返回第二小區,而不是一個在它與HREF的內容。因此,從下面的HTML示例代碼,我需要從

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

HTML代碼

觸發jquery的邏輯是在該行的第四單元中的按鈕提取21586,並且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代碼

//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()); 
    }); 

任何建議,將不勝感激。

+0

你爲什麼不分析你的ID HREF? –

+0

@YaWang我需要先通過jquery「找到」href ...然後我可以解析它 – dot

+0

在jQuery中'$ ='表示*「以*結尾,但href不以'row_id'結尾,它以'row_id = 21586'結尾。你是否在尋找一個過濾器來匹配密鑰和任何數字,或者你只是想找到那個數字,或者是什麼,它不是很清楚? – adeneo

回答

0

嘗試這個 。可以普遍用於任何網址和參數值。

$.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/

0

你可以試試這個:

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); 
}); 
0

檢查控制檯,它返回21586. 我用.closest().find()導航並找到相應的行<a>。然後我得到了href屬性,並把它分解找到所需id

$(document).ready(function() { 
 
    $('.icon').on('click', function() { 
 
    \t 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>

0

嘗試使用以下

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

//結果將等於21586