Clear input on focus with jQuery and return on blur

后端 未结 12 2322
耶瑟儿~
耶瑟儿~ 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:37

    You need to remove the ' ' marks around the defaultText variable in your set method (val).

    Try something along hte lines of

    $(function() {
        var defaultText = '';
        $('input[type=text]').focus(function() {
            defaultText = $(this).val();
            $(this).val('');
        });
        $('input[type=text]').blur(function() {
            $(this).val(defaultText); // NB removed the ' ' marks
            //echo // What are you trying to echo? 
         });
     });
    

提交回复
热议问题