jquery-clear-default-input-values-once

原文地址: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('');才正常

效果还不错。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值