简体   繁体   English

在 if 条件下满足值后增量计数器不会停止

[英]increment counter won't stop after value is met in if condition

For some reason when the condition is met which the counter stops at 4 the onclick function seems to still be add more price where it says if(j > 1 && j < 5) how can i stop the function once the condition is met.出于某种原因,当满足计数器停止在 4 的条件时,onclick 函数似乎仍在添加更多的价格,它说 if(j > 1 && j < 5) 一旦满足条件,我如何停止该函数。 Thanks谢谢

function increment_bathClick() {
        if(j<4)
            j++;

            console.log(j);
            jQuery('#bath').val(j);

            var booking_type = jQuery('#booking_type').val();

            if(j > 1 && j < 5){

                //payAmt = parseFloat(bathrooms[i][booking_type]);

                payAmt =  payAmt + addbath * j;

                updateAmount(0,payAmt);

            }else if(j > 1 && i > 1){

                payAmt = parseFloat(bedrooms[i][booking_type]);

                payAmt =  payAmt + addbath * j;

                updateAmount(0,payAmt);

            }else{
                updateAmount(0,payAmt);
            }
}

Add some brackets, indention alone doesn't help ;-)添加一些括号,单独缩进没有帮助;-)

function increment_bathClick() {
    if(j<4)
    {
        j++;

        console.log(j);
        jQuery('#bath').val(j);

        var booking_type = jQuery('#booking_type').val();

        if(j > 1 && j < 5){

            //payAmt = parseFloat(bathrooms[i][booking_type]);

            payAmt =  payAmt + addbath * j;

            updateAmount(0,payAmt);

        }else if(j > 1 && i > 1){

            payAmt = parseFloat(bedrooms[i][booking_type]);

            payAmt =  payAmt + addbath * j;

            updateAmount(0,payAmt);

        }else{
            updateAmount(0,payAmt);
        }
    }
}

Change this改变这个

if(j<4)
  j++;

to this对此

if(j<4){
   j++;
}else{
   return;
}

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

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