开发者

how to reset form in firefox browser by javascript

I want to reset form in firefox browser. When I use previous function in back button, the page is not reset for hidden field. It has previous stage. So, How do I?

<Html>
 <Head>
  <Title>My Testing for javascript</Title>
  <Script type="text/javascript">
   function hidetext(){
    window.alert('Start save to hidden');
    document.getElementById('hid').value = document.getElementById('puttextbox').value;
    window.alert('Complete save to hidden');
    document.getElementById('puttextbox').value='';
   }

   function displaytext(){
    window.alert('Start display from hidden');
    document.getElementById('displaytextbox').value = document.getElementById('hid').value;
    window.alert('Complete displ开发者_如何学Pythonay from hidden');
   }

   function resetform(){
    document.getElementById('form1').reset();
    window.alert('reset is completing.....');
   }

  </Script>
 </Head>
 <Body>
  <form id="form1">
  <div>
   Type your hidden text &nbsp;&nbsp;<input type="text" id="puttextbox"/>
   <br/>
   Display your hidden text &nbsp;&nbsp;<input type="text" id="displaytextbox"/>
   <br/>
   <input type="hidden" id="hid"/>
   <button type="button" id="putbutton" onclick="hidetext();">Put the textbox</button>
   <button type="button" id="displaybutton" onclick="displaytext();">Display hidden text</button>
   <button type="button" id="resetbutton" onclick="resetform();">Reset</button>
  </div>
  </form>
 </Body>
</Html>


Why not this:

<input type="reset" value="Reset">

? It's HTML-native. Or try this instead:

window.onload = function() {

var inputs = document.getElementsByTagName('input');

  for (var i = 0; i < inputs.length; i++) {
     inputs[i].value = '';
  }
};

It clears each input element in the page.


function clearForm(oForm) {

  var elements = oForm.elements; 

  oForm.reset();

  for(i=0; i<elements.length; i++) {

 field_type = elements[i].type.toLowerCase();

 switch(field_type) {

  case "text": 
  case "password": 
  case "textarea":
         case "hidden": 

   elements[i].value = ""; 
   break;

  case "radio":
  case "checkbox":
     if (elements[i].checked) {
       elements[i].checked = false; 
   }
   break;

  case "select-one":
  case "select-multi":
              elements[i].selectedIndex = -1;
   break;

  default: 
   break;
 }
    }
}

That should do the job.


The most simple version is:

document.form1.reset();

However, this resets all input fields to their default value, i.e. the one sent along with the HTML. If you want all fields cleared, you'd need to loop through them all.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜