How to increase the value of a quantity field with jQuery?

前端 未结 8 962
北荒
北荒 2021-01-07 05:25

I have a form with some quantity field and a plus and minus sign on each side,

    
product1
8条回答
  •  别那么骄傲
    2021-01-07 05:58

    I'm a rookie, but I did it like this for integers...

    min and max attributes are optional. Then, inside jQuery(document).ready(...

    $(".quantityMinus").live("click", function() {
      var qInput = $(this).parents(".quantityInput");
      var qText = qInput.find(".quantityText");
      var qValue = parseInt((qText.val())? qText.val() : 0);
      qText.val(Math.max(qValue - 1, (qInput.attr("min"))? qInput.attr("min") : -0xffff));
    });
    
    $(".quantityPlus").live("click", function() {
      var qInput = $(this).parents(".quantityInput");
      var qText = qInput.find(".quantityText");
      var qValue = parseInt((qText.val())? qText.val() : 0);
      qText.val(Math.min(qValue + 1, (qInput.attr("max"))? qInput.attr("max") : 0xffff));
    });
    

提交回复
热议问题