pass form name to jQuery

Asked

Viewed 623 times

0

I have the following function that sends a form:

function enviar_formulario() {
    document.search.submit();
}

When I want to call the function I do so:

        <select name='busca' onChange='enviar_formulario(search)'>
            <option value="1">ID</option>
            <option value="2">NOME</option> 
        </select>   

And whenever I select the option it sends the form.

Well it works, but the form has to be under the name of search. How to pass the name of the form to be sent to the function?

Example:

enviar_formulario(nome_do_formulário_a_ser_enviado)
  • You can show the form and button HTML?

  • Actually it’s not a button, it’s a select, and when I change the value it has to submit the form. I’ll put it in my question.

  • @Hugoborges You want to pass this code to Jquery and also use a mode as parameter ?

  • @Mauroalexandre put in pure JS because I don’t know how to do in Jquery hahahaha. Yes and that’s just what I want.

  • And where does the form name come from? By select options are fields.

  • then I want to inform the name when I call the function enviar_formulario(nome_aqui) o formulate this thus <form name='nome_aqui'>

Show 1 more comment

3 answers

3

If you used as I suggested in the other reply applies well here too:

$('select[name="busca"]').on('change', enviar_formulario);

function enviar_formulario() {
    document[this.name].submit();
}

Or just go look for the form where this select is by DOM hierarchy:

$('select[name="busca"]').on('change', function() {
    $(this).closest('form').submit();
}
  • 1

    +1 by the Closest

  • thanks for the help

2


With jQuery you check getting the event Submit of any form and then obtain the name his:

HTML:

<form name="form1" action="">
  <input type="text" value="Olá">
  <input type="submit" value="Go">
</form>

Javascript:

$("form").submit(function( event ) { 
  event.preventDefault();
  var nome = $(this).attr("name");
  alert(nome);
});

Jsfiddle: https://jsfiddle.net/kwx8tyx1/

Edited:

With select you can do it this way:

<select name='busca' onChange="enviar_formulario('form1')">
     <option value="1">ID</option>
     <option value="2">NOME</option> 
</select>

Javascript:

function enviar_formulario(nome){
    $("form[name='"+ nome + "']").submit();
}
  • I think it would be a bad practice, since the author is quoting the code in pure JS, but he taggou Jquery, let’s see what the author thinks.

  • 1

    "pass form name to jQuery" is in title with jQuery tag.

  • Yes. Why is the doubt in the air, is not very clear. Your question is good, I just quoted it so the author could see.

  • thanks for the help

0

Try to do the function like this:

function enviar_formulario(nome) {
  document[nome].submit();
}

UPDATE

I thought it would work that way to not change much of what you already use.

Try it like this then:

document.querySelector('select[name=busca]').addEventListener('change', function() {
  enviarFormulario('form'+this.value);
});

function enviarFormulario(nome) {
  form = document.querySelector('form[name='+nome+']');
  if(form) {
    form.submit();
  }
}
  • gave this error on the console TypeError: undefined is not an object (evaluating 'document[nome].submit')

  • I edited my question, after you take a look there please

Browser other questions tagged

You are not signed in. Login or sign up in order to post.