简体   繁体   English

防止输入量在JQuery中变为负数

[英]Prevent input quanty from going negative in JQuery

I have a feature on my current site that increments and decrements a value when you click the plus button or the minus button. 我在当前站点上有一个功能,当您单击加号按钮或减号按钮时,该功能会递增和递减值。 Since it's on an ecommerce site as an order quantity, it doesn't make a whole lot of sense to have an option for the value to be negative. 由于它是在电子商务网站上作为订单数量,因此选择价值为负数并没有多大意义。 I was trying to add some logic to my jquery that would prevent the value from becoming negative but my nested logic wasn't working for some reason. 我试图在我的jquery中添加一些逻辑,这会阻止值变为负数,但我的嵌套逻辑由于某种原因不起作用。 Any help would be much appreciated! 任何帮助将非常感激!

JQuery JQuery的

$(document).ready(function(e) {

$('.up').on('click',function(){
            var num = $(this).parent().find('.input-quantity').val();
            $(this).parent().find('.input-quantity').val(parseInt(num)+1);
        });
        $('.down').on('click',function(){
              var num = $(this).parent().find('.input-quantity').val();
            $(this).parent().find('.input-quantity').val(parseInt(num) -1);
        }); 

});

HTML HTML

 <div class="quantity clearfix">
    <span>quantity:</span>  
           <div  class="incre">
        <em class="down">-</em ><input type="text"  value="1" class="input1 input-quantity "><em class="up">+</em ><br>

</div>  
      <div class="clear"></div>  

   </div>

CSS CSS

.quantity                        {margin: 30px 0 0 0;max-width:242px;border: solid 1px #d3d2d2;padding: 3px 0 6px 11px;border-radius: 4px;}
.quantity  span                 {display:inline-block;float:left;font-family:Arial, Helvetica, sans-serif;font-size: 14px;line-height: 20px;padding: 6px 0 0 0;color: #333333;}
.incre                  {display:inline-block;float:right;margin: 0;width: 39%;}
.incre  em                  {display: inline-block;font-size: 26px;line-height: 31px;width: 29px;height: 29px;background: #cccccc;border-radius: 100%;color: #fff;padding: 0;float: left;text-align: center;margin: 0; cursor:pointer;}
.incre  .input1             {outline:none;border:0;display: inline-block;float: left;width: 20px;text-align: center;margin: 0 7px 0 3px;padding: 3px 0 0 0;font-size: 19px;height: 28px;line-height: 19px;color: #000;}
.quantity  small                {display:block;font-size: 18px;line-height: 20px;color: #000;padding: 0 12px 0 9px;}

What I've tried: 我尝试过的:

I've tried changing the functionality for the 'down' button in my jQuery to say that if the value equals zero when the function is executed, make the value equal to one and decrement the value. 我已经尝试更改jQuery中'down'按钮的功能,如果在执行函数时值等于零,则使值等于1并减小该值。 This hack would prevent the value from ever going negative. 这种黑客会阻止价值变得负面。

    $('.down').on('click',function(){
          var num = $(this).parent().find('.input-quantity').val();
     if(num = 0){num = 1}
        $(this).parent().find('.input-quantity').val(parseInt(num) -1);
    }); 

I also tried: 我也尝试过:

if(num === "0"){num === "1"}

before the statement that decrements the value since it seemed like the integer may have been returning as a string. 在递减值的语句之前,因为整数可能已经作为字符串返回。

Anyways, any help would be very appreciated! 无论如何,任何帮助将非常感谢!

使用输入type=number ,然后设置最小值

<input type="number" min="0">

It should be 它应该是

if(num == 0) {
  num = 1;
}

instead of num = 0 而不是num = 0

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

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