0

我正在尝试获取CID每个.vehicleID并将每个附加href.vehicle-actions类中&VID=[CID]

<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);
    });
});
4

3 回答 3

1

您查看a.vehicleID元素内部,但您应该查看其 parent 内部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')attr函数的版本,将函数作为第二个参数

于 2013-07-16T11:15:37.777 回答
0

这段piece代码可能不起作用,

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

$('.vehicle-actions',this)this 中不会识别 nextdiv.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);
});
于 2013-07-16T11:16:32.793 回答
0

您尚未在所有标签中指定车辆 ID,因此以下语句将仅返回一个元素。

$('.vehicleID').length

所以在所有标签中正确添加类。

于 2013-07-16T11:19:37.587 回答