1

これは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値を使用してアンカータグを見つけ、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);   
    }
4

2 に答える 2

1

attrセレクターとメソッドの間にセミコロンを使用しないでください。また、existingUrlが変数の場合は連結する必要があります。次のことを試してください。

$('a[href="'+existingUrl+'"]').attr('href', resultUrl);
于 2012-08-18T13:27:20.393 に答える
0

thishrefは属性ではなく、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
    }

2番目のインスタンスでは、直接使用できますexistingUrl

$(existingUrl).attr('href', resultUrl);
于 2012-08-20T07:52:45.370 に答える