Two ways to clear the form data in JS mode

Source: Internet
Author: User

The original: JS way to clear the form data two ways

Method 1: Traverse the page element/* Empty form contents  id: Form id*/ function clearform (ID) {    var objId = document.getElementById ( ID);    if (objId = = undefined) {        return;   }    for (var i = 0; i < objId.elements.length; i++) {        if (Objid.elements[i].type = = "text") {            OBJID.E Lements[i].value = "";       }        else if (Objid.elements[i].type = = "Passwor D ") {            Objid.elements[i].value =" ";       }    &NB Sp   else if (Objid.elements[i].type = = "Radio") {            objid.elements[i].checked = false;       }        else if (Objid.elements[i].type = = "checkbox") {  &nbs P         objid.elements[i].checked = false;       }       else if (Objid.elements[i].type = = "Select-one") {            Objid.elements[i].optio ns[0].selected = true;       }        else if (Objid.elements[i].type = = "select- Multiple ") {            for (var j = 0; J < Objid.elements[i].options.length; J + +) {&nbsp ;               objid.elements[i].options[j].selected = false;           }       }        else if (Objid.elements[i].type = = "textarea") {& nbsp           Objid.elements[i].value = ";       }       / /else if (Objid.elements[i].type = = "File") {       ////objid.elements[i].select ();    &NB Sp  ////document.selection.clear ();       ////For IE, Opera, Safari, chrome       //VAR file = objid.elements[i];       //if (file.outerhtml) {       //File.outerh TML = file.outerhtml;       //} else {       //File.value = ""; FF (including 3.5)        //}       //}   }}  Method 2: Add &nbsp using the Reset button page ;<!--used to clear the form data--><input type= "reset" name= "reset" style= "display:none;"/> Trigger reset Event $ ("Input[type=reset" "). Trigger (" click ");      

JS way to empty form data in two ways

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.