[英]find anchor tag using href value and change href value with new href value
這是HTML代碼
<td>
<a class="buttontext" href="/kkdmpcu/control/MilkDeliveryScheduleReport.txt?shipmentId=10306&reportTypeFlag=milkDeliverySchedule" target="_blank" onclick="javascript:setSelectedRoute(this, 10306);" title="Milk Delivery Schedule">Delivery Schedule</a>
</td>
<td>
<a class="buttontext" href="/kkdmpcu/control/KVGenerateTruckSheet.txt?shipmentId=10306&reportTypeFlag=abstract" target="_blank" onclick="javascript:setSelectedRoute(this, 10306);" title="Abstract Report">Route Abstract Report</a>
</td>
我有href值。 使用href值,我應該找到定位標記,並使用jquery將href值更改為新值。 這是我目前無法使用的代碼。
$('a[href$=(existingUrl)]'); // existingUrl is the href value I have
.attr('href', resultUrl); // resultUrl is the url that I need to replace with existingUrl.
//The whole code I have Right now
function setSelectedRoute(existingUrl, shipmentId) {
var updatedUrl = existingUrl.toString();
var facilityIndex = updatedUrl.indexOf("facilityId");
if(facilityIndex > 0){
updatedUrl = updatedUrl.substring(0, facilityIndex -1);
}
var form = $("input[value=" + shipmentId + "]").parent();
var routeId = form.find("option:selected").text();
var resultUrl = updatedUrl + "&facilityId=" + routeId;
alert(resultUrl);
$('a[href='+existingUrl+']').attr('href', resultUrl);
}
您不應該在選擇器和attr
方法之間使用分號,而且如果existingUrl
是變量,則應將其連接起來,請嘗試以下操作:
$('a[href="'+existingUrl+'"]').attr('href', resultUrl);
this
是不是href
屬性,它是a
標簽本身,也就是第一個錯誤,因此existingUrl
將獲得的對象,而不是。
像這樣更改代碼
function setSelectedRoute(existingUrl, shipmentId) {
var updatedUrl = $(existingUrl).attr('href'); // first instance
// or var updatedUrl = existingUrl.getAttribute('href');
var facilityIndex = updatedUrl.indexOf("facilityId");
if(facilityIndex > 0){
updatedUrl = updatedUrl.substring(0, facilityIndex -1);
}
var form = $("input[value=" + shipmentId + "]").parent();
var routeId = form.find("option:selected").text();
var resultUrl = updatedUrl + "&facilityId=" + routeId;
alert(resultUrl);
$('a[href="'+$(existingUrl).attr('href')+'"]').attr('href', resultUrl); //second instance
}
對於第二個實例,您可以直接使用existingUrl
$(existingUrl).attr('href', resultUrl);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.