Clear input on focus with jQuery and return on blur

后端 未结 12 2312
耶瑟儿~
耶瑟儿~ 2021-02-09 17:50

This almost works. However, when leaving the field \"defaulttext\" appears rather than the original text value. Not sure how to most efficiently echo the variable inside default

12条回答
  •  梦如初夏
    2021-02-09 18:35

    var q = $('#q');
    q.focus(function() {
        if ($(this).attr('data-default') == $(this).val()) {
            $(this).val('');
        }
    }).blur(function() {
        if($(this).val() == '') {
            $(this).val($(this).attr('data-default'));
        }
    });
    
    
    
    

提交回复
热议问题