1

I'm trying to reset a form, but it doesn't clear out the previous selections. So when I refresh the page and submit it remembers the old values and submits them. Even if I haven't made any selections on the form. This is the code:

<script>
 $(function(){ 
   $("#eventForm")[0].reset();
   });
</script>

Thanks

EDIT: The form

<form id="eventForm">            
     <select name="eventer" id="abc">
        <option value="" disabled="disabled" selected="selected">Select  Event</option>
        <option value="USA">US</option>
        <option value="UK">UK/option>
        <option value="Fr">France</option>
     </select>

    <input type="submit" id="open" onclick="heading()" value="Start" />
    <input type="button" id="close" onclick="closeAnalysis()" value="Stop"/>

I tried clearing them like this, but no success:

$(document).ready(
   function(){
   $("#close").click(
    function(){
     var values = [];
     var selected = $("select").each(
      function(){
      values.push( $(this).val());
      });
    this.form.reset();
    for (i=0;i<selected.length;i++)
    $(selected[i]).val(values[i]);
  });
  }
 );
user94628
  • 3,641
  • 17
  • 51
  • 88
  • 1
    see http://stackoverflow.com/questions/2002957/jquery-form-reset-exclude-select-box – djjeck Dec 05 '12 at 15:02
  • Could be browser-specific, what did you test it with? – roryf Dec 05 '12 at 15:02
  • 3
    @user94628: Ignore my answer, I answered quickly while suffering a sinus infection and my answer isn't even close to accurate or helpful. – Joel Etherton Dec 05 '12 at 15:03
  • @JoelEtherton: Ok no problems. – user94628 Dec 05 '12 at 15:04
  • .reset() will not necessarily clear the fields, it will reset them to the values that are provided with the `value="..."` attribute. – devnull69 Dec 05 '12 at 15:05
  • Ok, in my form there are two select tags each with several option tags. The selected option tag for both has a value="" and disabled="disabled" – user94628 Dec 05 '12 at 15:09
  • Does your form just post back to the same page, where you then clear the values? If so, refreshing the page will just post the old data again. Are you getting the browser warning? – Jack Dec 05 '12 at 15:15
  • Can you post the form HTML (as rendered when the `reset` is called), the expected outcome, and the actual outcome? – roryf Dec 05 '12 at 15:15
  • What's the point of selecting element at index 0 when calling $("#eventForm")[0].reset(); ? Shouldn't it be $("#eventForm").reset(); ? – Walialu Dec 05 '12 at 15:20
  • I've included the form data – user94628 Dec 05 '12 at 15:24
  • nevermind.. some specific jquery stuff I guess.. but you can do document.getElementById("eventForm").reset(); – Walialu Dec 05 '12 at 15:26
  • @Walialu Thanks I tried document.getElementById("eventForm").reset(); But doesnt work either – user94628 Dec 05 '12 at 15:26

1 Answers1

1

reset() returns the fields to the initial values set when the page is loaded. It will not remove the text. If you want to remove the text, you are going to have to loop through the elements and clear them.

epascarello
  • 204,599
  • 20
  • 195
  • 236