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

316
Visualizações
Wait for Sweet Alert input if a condition is met, otherwise continue

I have this script, which in some cases may require user input. It works, but the script continues regardless of the result of the Sweet Alert input.

$('#cart').on('click', '.button', function() {

    var code = $(this).data('code')

    if ((code !== '') && (code !== null) && (code !== undefined)) {

        var repeated = 0
        var secondary = null

        if (code.startsWith('ABC')) {
            swalBs.fire({
                text: 'Enter secondary code',
                showCancelButton: true,
                cancelButtonText: 'Cancel',
                showConfirmButton: true,
                confirmButtonText: 'Confirm',
                reverseButtons: true,
                input: 'text',
                inputValidator: (value) => {
                    if (!value) {
                        return 'Code required'
                    }
                }
            })
            .then((result) => {
                console.log(result)
                secondary = result.value
            })
        }

        if (repeated < 1) {
            $.post(serviceUrl, {
                c : code,
                s : secondary
            }, function(response) {
                ...
            })
            .fail(function() {
                ...
            })
        } else {
            swalBs.fire('Error', 'Code already entered', 'error')
        }

    } else {
        swalBs.fire('Enter code', 'Field must not be blank.')
    }

})

How can I make this script wait for the Sweet Alert input while still allowing the $.post to happen when the if (code.startsWith('ABC')) condition is not met (and Sweet Alert is not needed)?

about 4 years ago · Juan Pablo Isaza
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