How to clear a textbox onfocus?

前端 未结 2 1729
夕颜
夕颜 2020-12-29 23:51

I\'ve tried this but it doesn\'t work:

$(function() {
    $(\'input[type=text]\').focus(function() {
      $(this).val() == \'\';
      });
 });
相关标签:
2条回答
  • 2020-12-30 00:33

    A little late but another approach would be this

    link text

    When you click the input box it will select all the text(not clear) only if it is the default value on the box. That way if a user types something then has to go back to edit it, it's not cleared which could be quite annoying!

    Also selecting the text instead of clearing it reminds the user of the example. One to keep in mind i guess.

    0 讨论(0)
  • 2020-12-30 00:38

    To set the value, you have to pass the new value as a parameter. This is a funky thing with the .val() jQuery function.

    $(this).val('')
    

    take a look at the jQuery API and search for 'val'

    0 讨论(0)
提交回复
热议问题