How to clear all input fields in a specific div with jQuery?

后端 未结 9 1771
情歌与酒
情歌与酒 2020-12-12 22:11

I am trying to use jQuery to do a simple thing: clear the input fields of a group of input field within a div whenever the form loads, or if the user changes a picklist; but

9条回答
  •  萌比男神i
    2020-12-12 22:36

    This function is used to clear all the elements in the form including radio button, check-box, select, multiple select, password, text, textarea, file.

    function clear_form_elements(class_name) {
      jQuery("."+class_name).find(':input').each(function() {
        switch(this.type) {
            case 'password':
            case 'text':
            case 'textarea':
            case 'file':
            case 'select-one':
            case 'select-multiple':
            case 'date':
            case 'number':
            case 'tel':
            case 'email':
                jQuery(this).val('');
                break;
            case 'checkbox':
            case 'radio':
                this.checked = false;
                break;
        }
      });
    }
    

提交回复
热议问题