简体   繁体   English

jQuery查找最接近的类并增加价值

[英]jquery find closest class and increase value

I have buttons to increase/decrease quantity in a cart 我有按钮来增加/减少购物车中的数量

<div class="product-quantity">
  <button class="qtyminus">-</button>
  <input type="text" name="quantity" value="1" class="qty form-control">
  <button class="qtyplus">+</button>
</div>

my javascript unfortunately doesn't work can't figure out why. 不幸的是,我的JavaScript无法正常工作,无法弄清原因。

$('.qtyplus').on('click', function(e) {
    e.preventDefault();
    var num = Number($(this).closest('.qty').val());
    $(this).closest('.qty').val(++num);
});

jQuery closest searches ancestors, but in this case, you're looking for the sibling element. jQuery closest搜索祖先,但是在这种情况下,您正在寻找同级元素。 Try siblings instead of closest 尝试siblings而不是closest siblings

By the way, modern browsers have built-in debugging tools. 顺便说一句,现代的浏览器具有内置的调试工具。 It's easy to set a breakpoint and step through you code to see what's happening, and to use the console window to test things. 设置断点并逐步执行代码以查看正在发生的事情,并使用控制台窗口进行测试很容易。

You should use siblings() instead of closest() as closest() searches for ancestors while siblings() searches for siblings of an element. 您应该使用siblings()而不是closest()因为closest()搜索祖先,而siblings()搜索元素的同胞。

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="product-quantity"> <button class="qtyminus">-</button> <input type="text" name="quantity" value="1" class="qty form-control"> <button class="qtyplus">+</button> </div> <script> $('.qtyplus').on('click', function(e) { e.preventDefault(); var num = Number($(this).siblings('.qty').val()); $(this).siblings('.qty').val(++num); }); $('.qtyminus').on('click', function(e) { e.preventDefault(); var num = Number($(this).siblings('.qty').val()); $(this).siblings('.qty').val(--num); }); </script> 

Your issue is because closest() looks up the DOM, yet .qty is a sibling to the clicked buttons, so you need to use siblings() instead. 您的问题是因为closest()查找DOM,而.qty是单击按钮的同级对象,因此您需要改用siblings()

Also note that you can use a single event handler for both buttons if you put a common class on them and provide the value to add in a data attribute. 还要注意,如果在两个按钮上都放置了一个通用类,并提供了要添加到data属性中的值,则可以对两个按钮使用单个事件处理程序。 You can also negate the need to repeatedly select the same element by providing a function to val() which returns the new value based on its current one. 您还可以通过向val()提供一个函数来重复选择同一元素,该函数根据当前值返回新值。 Try this: 尝试这个:

 $('.amendqty').on('click', function(e) { e.preventDefault(); var inc = $(this).data('inc'); $(this).siblings('.qty').val(function(i, v) { return parseInt(v, 10) + inc; }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="product-quantity"> <button class="amendqty" data-inc="-1">-</button> <input type="text" name="quantity" value="1" class="qty form-control"> <button class="amendqty" data-inc="1">+</button> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM