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

107
Visualizações
javascript addEventListener will run more than once after several clicking

Click button will trigger setup function and add change event to input file, after choosing file , it will upload. The question is that it will run multiple times based on uploading times. I tried to reset the input file value after uploading , and still it will run twice in second times. How to fix this problem, I don't want to refresh page, and also I want it only run one time every click.

html code below

<input onclick='setup()' value='btn' type='button' class='btn btn-info'>
<form id='formid' method='POST' enctype='multipart/form-data'>
     <input accept='image/*' id='fileid' type='file' name='my_file' onsubmit='return false' />
     <input  type='submit' value='Submit' />
</form>

js code below

function setup(key) {
    document.getElementById('fileid').click();
    document.getElementById('fileid').addEventListener('change', submitForm);
    function submitForm() {
        var data = new FormData();
        data.append('my_file', $('#fileid').prop('files')[0]);
        $.ajax({
        url: 'ImageUpload.php',
        type: 'POST',               
        processData: false,
        contentType: false,
        data: data,
        success: function(data) {},
        error: function (error) {}
            });
        }
}
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