Reset Form with jquery, empty input error Select2 delete the list

0

I am using the following code:

$('#formarea')[0].reset();

This allows me to reset a form but in some cases it does not do it with the data that has been written with ajax or elements added with ajax.

I also empty the display none fields, there is some way to parameterize it so that it avoids the display none fields. also the fields with the select2 plugging are not sent to my php and before adding this line of code if I did it

Adding a class or cleaning fields by fields would mean editing a lot of code, it's a project I have and there are more than 120 different forms. the inputs follow the bootstrap design.

Current Code:

var formarea= $('#formarea *');
    formarea.not("[type=radio],[type=checkbox],[type=hidden],[readonly='readonly'],[style*='display: none'],.select2-offscreen,select").each(function(){
        var name = $(this).attr('name');
        console.log(name);
        $(this).val('');
    });

I changed the code since it did not have the scope I needed, now the situation is that when using it, it eliminates the options within the Select2

    
asked by Francisco Núñez 11.07.2017 в 17:14
source

2 answers

0

Well after breaking my head with the code, I got this:

$('#formarea :input').not("[type=radio],[type=checkbox],[type=hidden],[readonly='readonly'],[style*='display: none'],.select2-offscreen,[class^='select2'],select").each(function(){
        var name = $(this).attr('name');
        console.log(name);
        $(this).val('');
    });
    
answered by 11.07.2017 / 20:11
source
0

And instead of doing so much not, why do not you just include the ones you want? Here is an example with a couple of forms. I do not know if it's exactly what you want, but I think you can get something out.

What I do is, that the delete button looks in the form for the input text and equals its value to nothing.

$('#borrar').click(function(){borrar();});

function borrar(){
$( "form input:text" ).val('');
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
  <input type="button" value="Input Button">
  <input type="checkbox">
  <input type="file">
  <input type="hidden">
  <input type="image">
  <input type="password">
  <input type="radio">
  <input type="reset">
  <input type="submit">
  <input type="text" value="AAAAAAAAAAAAAAAAA">
  <select>
    <option>Option</option>
  </select>
  <textarea></textarea>
  <button>ENVIAR</button>
</form>
<br/>
<form>
  <input type="button" value="Input Button">
  <input type="checkbox">
  <input type="file">
  <input type="hidden">
  <input type="image">
  <input type="password">
  <input type="radio">
  <input type="reset">
  <input type="submit">
  <input type="text" value="AAAAAAAAAAAAAAAAA">
  <select>
    <option>Option</option>
  </select>
  <textarea></textarea>
  <button>ENVIAR</button>
</form>
<br/>
<br/>
<button id="borrar">BORRAR</button>
    
answered by 12.07.2017 в 10:36