如果用户选择输入文本,并且输入值为空字符串,我可以保留占位符吗? [英] Can I preserve placeholder if the user select the input text, and the input value is empty string?

查看:295
本文介绍了如果用户选择输入文本,并且输入值为空字符串,我可以保留占位符吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

输入可以设置占位符,但如果用户专注于该输入,占位符消失,我如何可以关闭占位符,util用户开始类型少一个字符?谢谢。

The input can set the placeholder, but if the user focus on that input , the place holder is disappear, how can I dismiss the placeholder, util the user start type at less one char?? Thank you.

推荐答案

不具有默认行为。但是,你可以通过添加一些JS到组合

Not with the default behavior. However, You could it do by adding some JS into the mix

Live fiddle: http://jsfiddle.net/jomanlk/gPBhX/

Live fiddle : http://jsfiddle.net/jomanlk/gPBhX/

<input type='text' class='placeholder' data-title='hello'>

$('.placeholder').each(function(){
    $(this).data('placeholder', $(this).attr('data-title'));
    $(this).val($(this).attr('data-title'));
});

$('.placeholder').live('keydown', function(){
    if ($(this).val() == $(this).data('placeholder')) {
        $(this).val('');         
    } 
});

$('.placeholder').live('blur', function(){
    if ($(this).val().trim() == '') {
        $(this).val($(this).data('placeholder'));
    }
});



注意:有一个限制,如果用户将一些文本粘贴到框中,但你可以通过绑定到更改事件很容易解决这个问题。这只是一个演示

Note : There's a limitation where if a user pastes some text into the box it won't clear, but you could easily fix that by binding to the change event as well. This is just a demo

这篇关于如果用户选择输入文本,并且输入值为空字符串,我可以保留占位符吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆