2013-07-16 72 views
0

我試圖讓每個.vehicleIDCID並追加每個href.vehicle-actions類中有&VID=[CID]jQuery的改變每個循環中的各子環節

<ul> 
      <li><a href="./garage_vehicle.php?mode=add_vehicle"><span>Create a Vehicle</span></a></li> 

      <li class="vehicle-view toggleVehicleButton"><a class="vehicleID" href="./garage_vehicle.php?mode=view_own_vehicle&amp;CID=6"><span>2014 Audi A4</span></a> 
       <div class="vehicle-actions" style="width: 179px; display: none;"> 
        <ul> 
         <li><a href="/board/garage_vehicle.php?mode=view_vehicle">View Vehicle</a></li> 
         <li><a href="/board/garage_vehicle.php?mode=edit_vehicle">Edit Vehicle</a></li> 
         <li><a class="deletevehicle" href="">Delete Vehicle</a></li> 
         <li><a href="/board/garage_modification.php?mode=add_modification">Add Modification</a></li> 
         <li><a href="/board/garage_premium.php?mode=add_premium">Add Insurance</a></li> 
         <li><a href="/board/garage_quartermile.php?mode=add_quartermile">Add Quartermile Time</a></li> 
         <li><a href="/board/garage_dynorun.php?mode=add_dynorun">Add Dynorun</a></li> 
         <li><a href="/board/garage_track.php?mode=add_lap">Add Lap</a></li> 
         <li><a href="/board/garage_service.php?mode=add_service">Add Service</a></li> 
         <li><a href="/board/garage_vehicle.php?mode=view_own_vehicle">Set Main Vehicle</a></li> 
        </ul> 
       </div> 
      </li> 

      <li class="vehicle-view toggleVehicleButton"><a class="vehicleID" href="./garage_vehicle.php?mode=view_own_vehicle&amp;CID=7"><span>2008 Chevrolet Corvette</span></a> 
       <div class="vehicle-actions" style="width: 179px;"> 
        <ul> 
         <li><a href="/board/garage_vehicle.php?mode=view_vehicle">View Vehicle</a></li> 
         <li><a href="/board/garage_vehicle.php?mode=edit_vehicle">Edit Vehicle</a></li> 
         <li><a class="deletevehicle" href="">Delete Vehicle</a></li> 
         <li><a href="/board/garage_modification.php?mode=add_modification">Add Modification</a></li> 
         <li><a href="/board/garage_premium.php?mode=add_premium">Add Insurance</a></li> 
         <li><a href="/board/garage_quartermile.php?mode=add_quartermile">Add Quartermile Time</a></li> 
         <li><a href="/board/garage_dynorun.php?mode=add_dynorun">Add Dynorun</a></li> 
         <li><a href="/board/garage_track.php?mode=add_lap">Add Lap</a></li> 
         <li><a href="/board/garage_service.php?mode=add_service">Add Service</a></li> 
         <li><a href="/board/garage_vehicle.php?mode=view_own_vehicle">Set Main Vehicle</a></li> 
        </ul> 
       </div> 
      </li> 
     </ul> 

在我的頭上,似乎簡單,但我可以」不像是會抓住我做錯了什麼

jQuery的

function getURLParameter(url, name) { 
    return (RegExp(name + '=' + '(.+?)(&|$)').exec(url)||[,null])[1]; 
} 

$('.vehicleID').each(function(i, val){ 
    var vehicleIDurl = ($(this).attr('href')); 
    var CID = getURLParameter(vehicleIDurl, 'CID'); 
    $('.vehicle-actions',this).each(function(i, val){ 
     editCID = $(this).find('a'); 
     editCID.attr('href', editCID.attr('href') + '&VID=' + CID); 
    }); 
}); 

回答

1

你看看a.vehicleID元素,但你應該看看它的父母li

您的代碼可以簡化。我這樣做:

$('.vehicleID').each(function(i, val){ 
    var CID = getURLParameter(this.href, 'CID'); 
    $('.vehicle-actions a', $(this).closest('li')).attr('href', function(_, href){ 
     if (href.indexOf("&VID")!=-1) return href; 
     return href + '&VID=' + CID); 
    }); 
}); 

其中簡化,我更喜歡在this.href$(this).attr('href')我用the version of the attr function taking a function as second parameter

+0

http://jsfiddle.net/Beardyco/dmSRd/1/仍然遇到問題的網址 – ngplayground

+0

你看到什麼問題? –

0

的代碼可能無法正常工作這piece

$('.vehicle-actions',this).each(function(i, val){ 
    editCID = $(this).find('a'); 
    editCID.attr('href', editCID.attr('href') + '&VID=' + CID); 
}); 

$('.vehicle-actions',this)這不會識別下一個div.vehicle-actions,因爲它不是a孩子,所以使用它closest li.vehicle-view作爲家長選擇

試試這個,

$('.vehicle-actions',$(this).closest('li.vehicle-view')).each(function(i, val){ 
    editCID = $(this).find('a'); 
    editCID.attr('href', editCID.attr('href') + '&VID=' + CID); 
}); 
+0

我已經使用過這個,但是當它們每個都有不同的url時,這會將所有的URL改爲'/board/garage_vehicle.php?mode = view_vehicle&VID = 7' – ngplayground

0

您尚未在所有標籤中指定vehicleID,低語句將只返回一個元素。

$('.vehicleID').length 

因此,在所有標籤中正確添加類。