我们有一个html输入,旁边有一个增加和减少按钮,通过点击将值更改为输入。现在的问题是,表单在点击后直接提交。
我们希望在用户停止点击时添加1秒的延迟。我们已经尝试了延迟功能,但我们面临的问题是,这直接创建了1秒的延迟。因此,当用户没有准备好点击时,1秒后表单仍然被提交。
我们想改变这一点,所以表单只在用户停止点击1秒后提交。
我们如何才能做到这一点?
HTML:
<div class="product-cart-qty-item" data-th="<?= $block->escapeHtml(__('Qty')) ?>">
<div class="product-cart-qty-block">
<button type="button" id="<?= /* @escapeNotVerified */ $_item->getId() ?>-dec" class="decreaseQty"><i class="far fa-minus"></i></button>
<input id="cart-<?= /* @escapeNotVerified */ $_item->getId() ?>-qty"
name="cart[<?= /* @escapeNotVerified */ $_item->getId() ?>][qty]"
data-cart-item-id="<?= /* @escapeNotVerified */ $_item->getSku() ?>"
value="<?= /* @escapeNotVerified */ $block->getQty() ?>"
type="number"
size="4"
title="<?= $block->escapeHtml(__('Qty')) ?>"
class="input-text qty"
data-validate="{required:true,'validate-greater-than-zero':true}">
<button type="button" id="<?= /* @escapeNotVerified */ $_item->getId() ?>-upt" class="increaseQty"><i class="far fa-plus"></i></button>
</div>
</div>
jQuery:
$('#<?php echo $_item->getId();?>-upt, #<?php echo $_item->getId();?>-dec').on("click",function(){
var $this = $(this);
var ctrl = ($(this).attr('id').replace('-upt','')).replace('-dec','');
var currentQty = $("#cart-"+ctrl+"-qty").val();
if($this.hasClass('increaseQty')){
var newAdd = parseInt(currentQty)+parseInt(1);
$("#cart-"+ctrl+"-qty").val(newAdd);
$('form#form-validate').submit();
}else{
if(currentQty>1){
var newAdd = parseInt(currentQty)-parseInt(1);
$("#cart-"+ctrl+"-qty").val(newAdd);
$('form#form-validate').submit();
}
}
});
2条答案
按热度按时间nom7f22z1#
很难编辑你的代码使它在一个例子中工作,因为它有这么多的PHP混合在其中。然而,这里有一个简化的例子,只有一个按钮,你点击增加一个数字,并在一段时间后,当停止点击它显示一条消息。我认为你可以适应这到你的代码。
基本上,在点击时,你需要启动一个计时器,并保存它。在每次点击时,你还必须清除任何以前的计时器,并重新保存新的计时器到同一个变量。
rbpvctlc2#
基本的反跳功能,使用超时,并清除它时,用户做另一个行动
用可重复使用的去抖方法制作