Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery动态设置href属性_Jquery_Attributes_Set - Fatal编程技术网

jQuery动态设置href属性

jQuery动态设置href属性,jquery,attributes,set,Jquery,Attributes,Set,我正在制作一个电子商务网站 当产品列表从数据库加载时,将其添加到购物车的链接如下所示(每个产品): 最后一行代码不工作。。。它最后显示了这样一个链接: <a data-id="10" class="addtocart fancybox" href="addtocart.php?id=undefined">Add to Cart!</a> 我试了很多,但都不管用。事先谢谢你试试这个: $(".addtocart").each(function () { $(thi

我正在制作一个电子商务网站

当产品列表从数据库加载时,将其添加到购物车的链接如下所示(每个产品):

最后一行代码不工作。。。它最后显示了这样一个链接:

<a data-id="10" class="addtocart fancybox" href="addtocart.php?id=undefined">Add to Cart!</a>
我试了很多,但都不管用。事先谢谢你

试试这个:

$(".addtocart").each(function () {
    $(this).attr("href", "addtocart.php?id=" + $(this).attr("data-id"));
});
$(".addtocart").prop("href", function() {
    return "addtocart.php?id=" + $(this).data("id");
});
href="addtocart.php?id=10"
$(".addtocart").each(function () {
    $(this).attr("href", "addtocart.php?id=" + $(this).attr("data-id"));
});
$(".addtocart").prop("href", function() {
    return "addtocart.php?id=" + $(this).data("id");
});