2012-01-26 39 views
1

我有一個帶有表格的頁面。該表包含包含錨的第th個元素。這是第一個例子是什麼樣子:使用JavaScript更新錨點

<th scope="col"> 
<a href="/Admin/Auction/Closed?sort=InventoryReference&sordir=ASC&sortdir=DESC">Inventory reference</a> 
</th> 

我想要做的就是更新的錨,使其有這樣一個額外的查詢字符串參數:

<th scope="col"> 
<a href="/Admin/Auction/Closed?sort=InventoryReference&sordir=ASC&sortdir=DESC&page=3">Inventory reference</a> 
</th> 

現在,我現在我必須這樣做:

<script language="javascript" type="text/javascript"> 

    $(document).ready(function() { 
     $('th[scope^=col]') ??? 
    }) 

</script> 

問題是雖然,我不知道應該取代什麼???。有人有什麼主意嗎?我想我需要一些抓取錨點的JavaScript或JQuery代碼,然後附加額外的查詢字符串參數。問題是代碼究竟是什麼?

感謝,

薩欽

回答

3

試試這個:

$(document).ready(function(){ 

    $('th[scope^=col]').each(function(){ 

     var old_link = ""; 
     var new_link = ""; 
     //Get the current href value of the child anchor tag of 'th[scope^=col]' 
     old_link = $(this).children('a').attr('href'); 
     //Append the extra string required '(in this case &page=3)' to the old_link 
     new_link = old_link + '&page=3'; 
     //Set the href value for the anchor tag within 'th[scope^=col]' 
     $(this).children('a').attr('href',new_link); 

    }); 

}); 

希望這有助於。

+0

感謝薩加爾 - 那是現貨。當我被允許時,我會在6分鐘內接受這個答案。 –

+0

如果你喜歡,你可以逐行解釋這段代碼:-) –

+0

這個問題有一個小問題:它會更新所有來自第一個數據鏈接的鏈接 - 如果你得到我想要的東西? –

3

試試這個

$(document).ready(function() { 
    $('th[scope=col] a')[0].href += "&page=3"; 
}); 

如果您認爲網址已經可以包含在查詢字符串page PARAM,你只需要更新它的值,然後使用此代碼。

$(document).ready(function() { 
    $a[0].href = $a[0].href.replace(/([?&])page=[^&]*&?/,'$1') + '&page=3'; 
}); 
+0

我不記得'href'是一個jQuery的方法.. –

+0

@RobW - 當時在我心裏我想,幸虧:P – ShankarSangoli

+0

沒有工作 –

1
$(document).ready(function() { 
    $('th[scope=col] a').each(function(){ 
     $this = $(this); 
     $this.attr('href', $this.attr('href') + '&page=3'); 
    }); 
});