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

后端 未结 9 1747
情歌与酒
情歌与酒 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条回答
  • 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;
        }
      });
    }
    
    0 讨论(0)
  • 2020-12-12 22:38

    Change this:

     <div class=fetch_results>
    

    To this:

     <div id="fetch_results">
    

    Then the following should work:

    $("#fetch_results input").each(function() {
          this.value = "";
      })​
    

    http://jsfiddle.net/NVqeR/

    0 讨论(0)
  • 2020-12-12 22:39

    Just had to delete all inputs within a div & using the colon in front of the input when targeting gets most everything.

    $('#divId').find(':input').val('');
    
    0 讨论(0)
提交回复
热议问题