gpt4 book ai didi

jquery - 单击时保持 Bootstrap 下拉菜单打开

转载 作者:IT王子 更新时间:2023-10-29 03:26:16 26 4
gpt4 key购买 nike

我使用 Bootstrap 下拉菜单作为购物车。在购物车中有一个“删除产品”按钮(链接)。如果我单击它,我的购物车脚本会删除产品,但菜单会消失。无论如何有办法防止这种情况吗?我尝试了 e.startPropagation,但似乎没有用:

<div id="shoppingcart" class="nav-collapse cart-collapse">
<ul class="nav pull-right">
<li class="dropdown open">
<a href="#" data-toggle="dropdown" class="dropdown-toggle">Totaal:
&acirc;&sbquo;&not; 43,00</a>

<ul class="dropdown-menu">
<li class="nav-header">Pakketten</li>

<li>
<span class="quantity">1x</span>
<span class="product-name">Test Product </span>
<span class="product-remove"><a class="removefromcart" packageid="4" href="#">x</a>
</span></li>

<li><a href="#">Total: &euro; 43,00</a></li>

<li><a href="/checkout">Checkout</a></li>
</ul>
</li>
</ul>

如您所见,带有 class="dropwdown-toggle"的元素使其成为下拉列表。另一个想法是我只是在以编程方式单击时重新打开它。因此,如果有人可以向我解释如何以编程方式打开 Bootstrap 下拉菜单,那将会很有帮助!

最佳答案

像这样尝试移除按钮本身的传播:

$('.dropdown-menu a.removefromcart').click(function(e) {
e.stopPropagation();
});

编辑

下面是来自上述解决方案评论的演示:

http://jsfiddle.net/andresilich/E9mpu/

相关代码:

JS

$(".removefromcart").on("click", function(e){
var fadeDelete = $(this).parents('.product');
$(fadeDelete).fadeOut(function() {
$(this).remove();
});

e.stopPropagation();
});

HTML

<div id="shoppingcart" class="nav-collapse cart-collapse">
<ul class="nav pull-right">
<li class="dropdown open">
<a href="#" data-toggle="dropdown" class="dropdown-toggle">Totaal:
&acirc;&sbquo;&not; 43,00</a>

<ul class="dropdown-menu">
<li class="nav-header">Pakketten</li>
<li class="product">
<span class="product-remove"><a class="removefromcart" packageid="2" href="#"><i class="icon-remove"></i></a></span>
<span class="product-name">Test Product </span>
<span class="quantity"><span class="badge badge-inverse">1</span></span>
</li>
<li class="product">
<span class="product-remove"><a class="removefromcart" packageid="2" href="#"><i class="icon-remove"></i></a></span>
<span class="product-name">Test Product </span>
<span class="quantity"><span class="badge badge-inverse">10</span></span>
</li>
<li class="product">
<span class="product-remove"><a class="removefromcart" packageid="2" href="#"><i class="icon-remove"></i></a></span>
<span class="product-name">Test Product </span>
<span class="quantity"><span class="badge badge-inverse">8</span></span>
</li>
<li class="product">
<span class="product-remove"><a class="removefromcart" packageid="2" href="#"><i class="icon-remove"></i></a></span>
<span class="product-name">Test Product </span>
<span class="quantity"><span class="badge badge-inverse">3</span></span>
</li>
<li class="product">
<span class="product-remove"><a class="removefromcart" packageid="2" href="#"><i class="icon-remove"></i></a></span>
<span class="product-name">Test Product </span>
<span class="quantity"><span class="badge badge-inverse">4</span></span>
</li>
<li class="divider"></li>
<li><a href="#">Total: &euro; 43,00</a></li>
<li><a href="/checkout">Checkout</a></li>
</ul>
</li>
</ul>

关于jquery - 单击时保持 Bootstrap 下拉菜单打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10480697/

26 4 0