这是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 值,我应该找到 anchor 标记并使用 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);
关于javascript - 使用 href 值查找 anchor 标记并使用新的 href 值更改 href 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12018925/