gpt4 book ai didi

javascript - 使用 href 值查找 anchor 标记并使用新的 href 值更改 href 值

转载 作者:行者123 更新时间:2023-11-28 02:45:08 25 4
gpt4 key购买 nike

这是html代码:

<td>
<a class="buttontext" href="/kkdmpcu/control/MilkDeliveryScheduleReport.txt?shipmentId=10306&amp;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&amp;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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com