2017-06-22 102 views
0

我有一個鏈接,打開一個包含信息的頁面。我需要獲取這些信息並將其顯示在另一頁的一部分中。我有一個鏈接使用一個標記來獲取適當的鏈接,因爲每個頁面都不同。一個示例鏈接如下所示:抓取網址的結尾文本href

<a id="ItemDetail_SpecPageLink" style="display: none;" class="ItemDetailLocAvail" href="/customer/wetosu/specpages/FactoryStock.aspx?item=65048"></a> 

鏈接的唯一部分更改是item=後的所有內容。我開始編寫我知道的東西,但我不知道如何在代碼中編寫「在item=之後抓取所有內容」並將其設置爲var。我無法在這裏找到像我這樣的問題,因爲每個人都試圖找到與我的鏈接相同的特定文本,我需要抓取item=之後的任何內容。

我用這個編碼來獲取信息,我知道我的VAR factoryLink抓住整個鏈路,但是這是我卡上的一部分,我不想離開它爲虛無

//GET FACTORY AVAILABILITY 
var factoryLink = $('#ItemDetail_SpecPageLink').attr('href'); 

$.ajax({ 
    url: ('/customer/wetosu/specpages/FactoryStock.aspx?item=' + factoryLink), 
    type: 'GET', 
    success: function (data) { 
     $('#FactoryAvailability').html(data); 
    } 
}); 

任何幫助是明智的!

+0

的可能的複製[從jquery的HREF獲取參數值](https://stackoverflow.com/questions/15780717/get-parameter-values-from- HREF合的jquery) – H77

回答

1

你可以使用正則表達式:

item=(.+)$ 

正則表達式崩潰:

  • item=你想要什麼第一部分匹配
  • (capture group
  • .+開始說比賽無限次之後的任何事情
  • )捕獲組
  • $字符串

The result of String.prototype.match is an array的端部的端部,第一個參數是整個匹配和第一捕獲組中的第二個參數。

希望這會有所幫助。

var factoryLink = $('#ItemDetail_SpecPageLink').attr('href'); 
 

 
var yourId = factoryLink.match(/item=(.+)$/)[1]; 
 

 
console.log({yourId}); 
 

 
// do what you want with it 
 
// $.ajax({ 
 
// url: ('/customer/wetosu/specpages/FactoryStock.aspx?item=' + yourId), 
 
// type: 'GET', 
 
// success: function(data) { 
 
//  $('#FactoryAvailability').html(data); 
 
// } 
 
// });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
    <a id="ItemDetail_SpecPageLink" class="ItemDetailLocAvail" href="/customer/wetosu/specpages/FactoryStock.aspx?item=65048">Some link</a>

Read more about javascript's regex on MDN.