jQuery 如何清除焦点上的文本框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1023135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to clear a textbox onfocus?
提问by sarmenhb
I've tried this but it doesn't work:
我试过这个,但它不起作用:
$(function() {
$('input[type=text]').focus(function() {
$(this).val() == '';
});
});
回答by Jon Erickson
To set the value, you have to pass the new value as a parameter. This is a funky thing with the .val() jQuery function.
要设置该值,您必须将新值作为参数传递。这是 .val() jQuery 函数的一个时髦的东西。
$(this).val('')
take a look at the jQuery APIand search for 'val'
查看jQuery API并搜索“val”
回答by Nooshu
A little late but another approach would be this
有点晚了,但另一种方法是这样
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.
同时选择文本而不是清除它提醒用户该示例。我猜要记住一个。