jquery-clear-default-input-values-once

来源:互联网 发布:嵌入式linux启动美化 编辑:程序博客网 时间:2024/06/10 11:09

原文地址:http://www.jotlab.com/2010/jquery-clear-default-input-values-once

中文搜索这个出不来,不知道怎么描述了,直接用英文题目了。就是清除表单默认值一次。。。。

I like to have default values in my text input fields. However it is an annoying user experience to have to manually clear the value out and then enter in the value. Instead I thought I would write a simple script that would work site wide to clear out the default values. You of course will need your jquery included:

Following some brilliant comments on this there is a much quicker method:

1
2
3
$('input[type=text]').one('focus', function(){
    $(this).attr('value', '');
});

However the original code I came up with is as follows:

1
2
3
4
5
6
7
8
$(function(){
    $('input[type=text]').focus(function(){
        if (!this.clicked) {
            $(this).attr('value', '');
            this.clicked = true;   
        }
    });
});

This will clear the fields out only once. So if the user enters a value, then focuses back on the field, their values won’t be cleared. So if they needed to edit a single letter, then their entire entry won’t be wiped. If you have any hints feel free to comment

然后,我改了一点东西,因为我有密码输入也需要清空。

$(function(){
    $('input').focus(function(){
        if (!this.clicked) {
            $(this).attr('value', '');
            this.clicked = true;   
        }
    });
});

不知道为什么   $(this).attr('value', '');在IE10下面不管用,然后改成$(this).val('');才正常

效果还不错。。。

原创粉丝点击