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

后端 未结 9 1764
情歌与酒
情歌与酒 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:33

    Fiddle: http://jsfiddle.net/simple/BdQvp/

    You can do it like so:

    I have added two buttons in the Fiddle to illustrate how you can insert or clear values in those input fields through buttons. You just capture the onClick event and call the function.

    //Fires when the Document Loads, clears all input fields
    $(document).ready(function() {
      $('.fetch_results').find('input:text').val('');    
    });
    
    //Custom Functions that you can call
    function resetAllValues() {
      $('.fetch_results').find('input:text').val('');
    }
    
    function addSomeValues() {
      $('.fetch_results').find('input:text').val('Lala.');
    }
    

    Update:

    Check out this great answer below by Beena as well for a more universal approach.

提交回复
热议问题