You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求修改jQuery数量增减脚本以实现自定义最小数量逻辑

没问题!我已经根据你的需求修改了原jQuery脚本,完美实现你要的自定义最小数量逻辑,同时保留了原脚本的所有核心功能。下面是具体的实现方案:

实现的核心逻辑对应你的需求

  • 当input的min设为300时:输入框值为0时点击“+”直接跳到300,之后每次点击按step累加(300→350→400…)
  • 当min设为0时:点击“+”按step正常递增(0→50→100…)
  • 点击“-”时:若当前值大于min则按step递减;若当前值等于min,点击后直接清零(比如min=300时,300→0)

修改后的完整脚本

function wcqib_refresh_quantity_increments() { 
  jQuery("div.quantity:not(.buttons_added), td.quantity:not(.buttons_added)").each(function(a, b) { 
    var c = jQuery(b); 
    c.addClass("buttons_added"), 
    c.children().first().before('<input type="button" value="-" class="minus" />'), 
    c.children().last().after('<input type="button" value="+" class="plus" />') 
  }) 
}
String.prototype.getDecimals || (String.prototype.getDecimals = function() { 
  var a = this, b = ("" + a).match(/(?:\.(\d+))?(?:[eE]([+-]?\d+))?$/); 
  return b ? Math.max(0, (b[1] ? b[1].length : 0) - (b[2] ? +b[2] : 0)) : 0 
}), 
jQuery(document).ready(function() { wcqib_refresh_quantity_increments() }), 
jQuery(document).on("updated_wc_div", function() { wcqib_refresh_quantity_increments() }), 
jQuery(document).on("click", ".plus, .minus", function() { 
  var a = jQuery(this).closest(".quantity").find(".qty"), 
      b = parseFloat(a.val()), 
      c = parseFloat(a.attr("max")), 
      d = parseFloat(a.attr("min")), 
      e = a.attr("step"); 
  
  // 初始化默认值处理
  b && "" !== b && "NaN" !== b || (b = 0), 
  "" !== c && "NaN" !== c || (c = ""), 
  "" !== d && "NaN" !== d || (d = 0), 
  "any" !== e && "" !== e && void 0 !== e && "NaN" !== parseFloat(e) || (e = 1); 

  // 处理"+"按钮点击逻辑
  if (jQuery(this).is(".plus")) {
    if (b === 0 && d > 0) {
      // 当前值为0且min>0时,直接设置为min值
      a.val(d);
    } else {
      // 正常累加,不超过max限制
      if (c && b >= c) {
        a.val(c);
      } else {
        a.val((b + parseFloat(e)).toFixed(e.getDecimals()));
      }
    }
  } 
  // 处理"-"按钮点击逻辑
  else {
    if (b === d) {
      // 当前值等于min时,直接清零
      a.val(0);
    } else if (b > d) {
      // 当前值大于min时,按step递减,确保不低于min
      let newVal = b - parseFloat(e);
      a.val(newVal < d ? d : newVal.toFixed(e.getDecimals()));
    } else {
      // 当前值已经小于min(比如手动输入的情况),直接清零
      a.val(0);
    }
  }

  // 触发输入框change事件,确保其他依赖逻辑生效
  a.trigger("change");
});

对应的表单代码示例

<div class="quantity buttons_added">
 <input type="button" value="-" class="minus">
 <input type="number" step="50" min="300" max="" id="1234567" name="1234567" readonly value="0" title="Quantity" class="input-text qty text" size="6" pattern="" inputmode="">
 <input type="button" value="+" class="plus">
</div>

你可以根据实际需求调整input的min和step属性,脚本会自动适配对应的逻辑。比如把min设为0时,就会回到正常的递增逻辑。

内容的提问来源于stack exchange,提问作者dabri

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 10:57:34