jQuery javascript清除字段值输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18645357/
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
javascript clear field value input
提问by
I am making a simple form and i have this code to clear the initial value:
我正在制作一个简单的表格,我有这个代码来清除初始值:
Javascript:
Javascript:
function clearField(input) {
input.value = "";
};
html:
html:
<input name="name" id="name" type="text" value="Name" onfocus="clearField(this);"/>
But what i don't want is that if the user fills the input but clicks it again, it gets erased. I want the field to have the starter value "Name" only if the input is empty. Thank you in advance!
但我不想要的是,如果用户填写输入但再次单击它,它会被删除。我希望该字段只有在输入为空时才具有起始值“名称”。先感谢您!
采纳答案by lekksi
Here is one solution with jQuery for browsers that don't support the placeholder attribute.
这是针对不支持占位符属性的浏览器使用 jQuery 的一种解决方案。
$('[placeholder]').focus(function() {
var input = $(this);
if (input.val() == input.attr('placeholder')) {
input.val('');
input.removeClass('placeholder');
}
}).blur(function() {
var input = $(this);
if (input.val() == '' || input.val() == input.attr('placeholder')) {
input.addClass('placeholder');
input.val(input.attr('placeholder'));
}
}).blur();
Found here: http://www.hagenburger.net/BLOG/HTML5-Input-Placeholder-Fix-With-jQuery.html
在这里找到:http: //www.hagenburger.net/BLOG/HTML5-Input-Placeholder-Fix-With-jQuery.html
回答by mithunsatheesh
do like
喜欢
<input name="name" id="name" type="text" value="Name"
onblur="fillField(this,'Name');" onfocus="clearField(this,'Name');"/>
and js
和js
function fillField(input,val) {
if(input.value == "")
input.value=val;
};
function clearField(input,val) {
if(input.value == val)
input.value="";
};
update
更新
here is a demo fiddleof the same
这是一个相同的演示小提琴
回答by cssyphus
This may be what you want:
这可能是你想要的:
This code places a default text string
Enter your name here
inside the<input>
textbox, and colorizes the text to light grey.As soon as the box is clicked, the default text is cleared and text color set to black.
If text is erased, the default text string is replaced and light grey color reset.
此代码
Enter your name here
在<input>
文本框中放置一个默认文本字符串,并将文本着色为浅灰色。单击该框后,将清除默认文本并将文本颜色设置为黑色。
如果文本被擦除,默认文本字符串将被替换并重置浅灰色。
HTML:
HTML:
<input id="fname" type="text" />
jQuery/javascript:
jQuery/javascript:
$(document).ready(function() {
var curval;
var fn = $('#fname');
fn.val('Enter your name here').css({"color":"lightgrey"});
fn.focus(function() {
//Upon ENTERING the field
curval = $(this).val();
if (curval == 'Enter your name here' || curval == '') {
$(this).val('');
$(this).css({"color":"black"});
}
}); //END focus()
fn.blur(function() {
//Upon LEAVING the field
curval = $(this).val();
if (curval != 'Enter your name here' && curval != '') {
$(this).css({"color":"black"});
}else{
fn.val('Enter your name here').css({"color":"lightgrey"});
}
}); //END blur()
}); //END document.ready
回答by Tomjr260
HTML:
HTML:
<input name="name" id="name" type="text" value="Name" onfocus="clearField(this);" onblur="fillField(this);"/>
JS:
JS:
function clearField(input) {
if(input.value=="Name") { //Only clear if value is "Name"
input.value = "";
}
}
function fillField(input) {
if(input.value=="") {
input.value = "Name";
}
}
回答by Ravinath Edirisinghe
var input= $(this);
input.innerHTML = '';