How can I clear the input text after clicking

后端 未结 14 1882
遥遥无期
遥遥无期 2021-01-31 07:26

Using jQuery, how can I clear the input text after I made a click? By default, the value remains in the input field.

For example, I have an input text and the value is <

14条回答
  •  独厮守ぢ
    2021-01-31 07:52

    To remove the default text, on clicking the element:

    $('input:text').click(
        function(){
            $(this).val('');
        });
    

    I would, though, suggest using focus() instead:

    $('input:text').focus(
        function(){
            $(this).val('');
        });
    

    Which responds to keyboard events too (the tab key, for example). Also, you could use the placeholder attribute in the element:

    
    

    Which will clear on focus of the element, and reappear if the element remains empty/user doesn't input anything.

提交回复
热议问题