单击后如何清除输入文本

单击后如何清除输入文本

问题描述:

使用jQuery,单击后如何清除输入文本?默认情况下,该值保留在输入字段中.

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

例如,我有一个输入文本,值是TEXT.单击时,我希望输入字段为空.

For example, I have an input text and the value is TEXT. When I perform a click, I want the input field to become empty.

要删除默认文本,请单击以下元素:

To remove the default text, on clicking the element:

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

不过,我建议改用focus():

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

哪个也会响应键盘事件(例如,Tab键).另外,您可以在元素中使用placeholder属性:

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

<input type="text" placeholder="default text" />

这将清除元素的焦点,并在元素保持空白/用户未输入任何内容时重新出现.

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