Js empty form data in two ways (traversal + reset) _ javascript tips-js tutorial

Source: Internet
Author: User
This article mainly introduces two methods (traversal + reset) for js to clear form data. For more information, see Method 1: traverse page elements.

/* Clear FORM Content 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. elements [I]. value = "";} else if (objId. elements [I]. type = "password") {objId. elements [I]. value = "";} else if (objId. elements [I]. type = "radio") {objId. elements [I]. checked = false;} else if (objId. elements [I]. type = "checkbox") {objId. elements [I]. checked = false;} else if (objId. elements [I]. type = "select-one") {objId. elements [I]. options [0]. selected = true;} else if (objId. elements [I]. type = "select-multiple") {for (var j = 0; j <objId. elements [I]. options. length; j ++) {objId. elements [I]. options [j]. selected = false ;}} else if (objId. elements [I]. type = "textarea") {objId. elements [I]. value = "";} // else if (objId. elements [I]. type = "file") {// objId. elements [I]. select (); // document. selection. clear (); // for IE, Opera, Safari, Chrome // var file = objId. elements [I]; // if (file. outerHTML) {// file. outerHTML = file. outerHTML; //} else {// file. value = ""; // FF (including 3.5 )//}//}}}

Method 2: Use the reset button

Add

 

Trigger reset events

$("input[type=reset]").trigger("click");

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.