Can I preserve placeholder if the user select the input text, and the input value is empty string?

故事扮演 提交于 2019-12-08 13:31:36

问题


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.


回答1:


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

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




回答2:


Using the placeholder attribute, you can't.



来源:https://stackoverflow.com/questions/5839573/can-i-preserve-placeholder-if-the-user-select-the-input-text-and-the-input-valu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!