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

216
Visualizações
My button doesn't send her form but another form

I have 2 forms in my html code, but when I go to press the button of form2, the value of form1 is sent

  function chk() {
    var text = document.getElementById('text').value;
    console.log(text);

    return false;
  }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<form id="form1">
  <input type="text" id="text" value="Hi">
  <input type="submit" id="submit1" onclick="return chk()">
  <label for="submit1">Hi</label>
</form>

<form id="form2">
  <input type="text" id="text" value="bonjour">
  <button id="submit2" onclick="return chk()">btn</button>
  <label for="submit2">bonjour</label>
</form>

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

0

Ids should be unique on a page. You can use a name for the input elements instead.

function chk(formId) {
  var text = document.getElementById(formId).querySelector('input[name=text]').value;
  console.log(text);
  return false;
}
<form id="form1">
  <input type="text" name="text" value="Hi">
  <input type="submit" id="submit1" onclick="return chk('form1')">
  <label for="submit1">Hi</label>
</form>
<form id="form2">
  <input type="text" name="text" value="bonjour">
  <button id="submit2" onclick="return chk('form2')">btn</button>
  <label for="submit2">bonjour</label>
</form>

about 4 years ago · Juan Pablo Isaza Relatório

0

the id "text" is used twice in your code, which can lead to confusion, you should try changing it to "text1" and "text2" And use the one you need in your function.

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