Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

108
Visualizações
How to number increment with limit

I am using number field with plus and minus for product quantity select for a project. I need to set a max limit when I press plus icon. I am using below code

$(document).ready(function() {
    $('.minus').click(function () {
        var $input = $(this).parent().find('input');
        var count = parseInt($input.val()) - 1;
        count = count < 1 ? 1 : count;
        $input.val(count);
        $input.change();
        return false;
    });
    $('.plus').click(function () {
        var $input = $(this).parent().find('input');
        $input.val(parseInt($input.val()) + 1 );
        $input.change();
       
        return false;
    });
});

How to set a maximum limit for when i press + icon.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Optimize for readability by using Math.min() and Math.max()

const MINIMUM = 1
const MAXIMUM = 99

$(document).ready(function() {
        $('.minus').click(function () {
            var $input = $(this).parent().find('input');
            var newCount = Math.max(parseInt($input.val()) - 1, MINIMUM);
            $input.val(newCount);
            $input.change();
            return false;
        });
        $('.plus').click(function () {
            var $input = $(this).parent().find('input');
            var newCount = Math.min(parseInt($input.val()) + 1, MAXIMUM)
            $input.val(newCount);
            $input.change();
            return false;
        });
    });
about 4 years ago · Juan Pablo Isaza Relatório

0

Here's how you'd limit it to 99 for example :

$(document).ready(function() {
    $('.minus').click(function () {
        var $input = $(this).parent().find('input');
        var count = parseInt($input.val()) - 1;
        count = count < 1 ? 1 : count;
        $input.val(count);
        $input.change();
        return false;
    });
    $('.plus').click(function () {
         var $input = $(this).parent().find('input');
        var count = parseInt($input.val()) + 1;
        count = count > 99 ? 99 : count;
        $input.val(count);
        $input.change();
        return false;
    });
});
about 4 years ago · Juan Pablo Isaza Relatório

0

You need to check the current value. If the value exceeds the limit, ignore the action:

$(document).ready(function() {
        $('.minus').click(function () {
            var $input = $(this).parent().find('input');
            var count = parseInt($input.val()) - 1;
            count = count < 1 ? 1 : count;
            $input.val(count);
            $input.change();
            return false;
        });
        $('.plus').click(function () {
            var $input = $(this).parent().find('input');

            if ($input.val() > 100) {
                return;
            }

            $input.val(parseInt($input.val()) + 1 );
            $input.change();
           
            return false;
        });
    });
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda