如何删除焦点输入的默认值 [英] How to remove default value of input on focus
本文介绍了如何删除焦点输入的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个输入框,该输入框已为其分配了默认值文本.当用户专注于该字段时,如何删除此文本:
I have an input box that has default value text assigned to it. How can I remove this text when the user focuses on the field::
CoDE
<input type="text" name="kp1_description" value="Enter Keypress Description">
推荐答案
$(document).ready(function(){
var Input = $('input[name=kp1_description]');
var default_value = Input.val();
Input.focus(function() {
if(Input.val() == default_value) Input.val("");
}).blur(function(){
if(Input.val().length == 0) Input.val(default_value);
});
})
应该这样做.
已更新,忘记了焦点没有焦点焦点事件的第二个参数,因为没有焦点,它必须与模糊链接在一起:
Updated, Forgot that focus does not have a 2nd parameter for the focus-out event because there is none, it has to be chained with blur:
您还应该考虑为此创建自己的函数,例如:
you should also think about creating your own function for this such as:
$.fn.ToggleInputValue = function(){
return $(this).each(function(){
var Input = $(this);
var default_value = Input.val();
Input.focus(function() {
if(Input.val() == default_value) Input.val("");
}).blur(function(){
if(Input.val().length == 0) Input.val(default_value);
});
});
}
然后像这样使用
$(document).ready(function(){
$('input').ToggleInputValue();
})
这篇关于如何删除焦点输入的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文