Clear form fields with jQuery

前端 未结 30 3128
甜味超标
甜味超标 2020-11-22 15:48

I want to clear all input and textarea fields in a form. It works like the following when using an input button with the reset class:

$(\".reset         


        
30条回答
  •  萌比男神i
    2020-11-22 16:34

    Let us say if you want to clear the fields and except accountType,in the mean time dropdown box will be reset to particular value,i.e 'All'.Remaining fields should be reset to empty i.e text box.This approach will be used for clearing particular fields as our requirement.

     $(':input').not('#accountType').each( function() {
    
        if(this.type=='text' || this.type=='textarea'){
                 this.value = '';
           }
        else if(this.type=='radio' || this.type=='checkbox'){
             this.checked=false;
          }
             else if(this.type=='select-one' || this.type=='select-multiple'){
                  this.value ='All';
         }
     });
    

提交回复
热议问题