[英]HTML how to clear input using javascript?
I have this INPUT, it will clear everytime we click inside of it.我有这个输入,每次我们点击它时它都会清除。
The problem: I want to clear only if value = exemplo@exemplo.com问题:我只想在 value = exemlo@exemplo.com 时清除
<script type="text/javascript">
function clearThis(target) {
target.value= "";
}
</script>
<input type="text" name="email" value="exemplo@exemplo.com" size="30" onfocus="clearThis(this)">
Can someone help me to do this?有人可以帮我做到这一点吗? I don't know how to compare, I already tried but no success.我不知道如何比较,我已经尝试过但没有成功。
<script type="text/javascript">
function clearThis(target) {
if (target.value == 'exemplo@exemplo.com') {
target.value = "";
}
}
</script>
Is this really what your looking for?这真的是你要找的吗?
For me this is the best way:对我来说,这是最好的方法:
<form id="myForm">
First name: <input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br><br>
<input type="button" onclick="myFunction()" value="Reset form">
</form>
<script>
function myFunction() {
document.getElementById("myForm").reset();
}
</script>
you can use attribute placeholder
您可以使用属性placeholder
<input type="text" name="email" placeholder="exemplo@exemplo.com" size="30" />
or try this for older browsers或者在旧浏览器上试试这个
<input type="text" name="email" value="exemplo@exemplo.com" size="30" onblur="if(this.value==''){this.value='exemplo@exemplo.com';}" onfocus="if(this.value=='exemplo@exemplo.com'){this.value='';}">
You could use a placeholder because it does it for you, but for old browsers that don't support placeholder, try this:您可以使用占位符,因为它适合您,但对于不支持占位符的旧浏览器,请尝试以下操作:
<script>
function clearThis(target) {
if (target.value == "exemplo@exemplo.com") {
target.value = "";
}
}
function replace(target) {
if (target.value == "" || target.value == null) {
target.value == "exemplo@exemplo.com";
}
}
</script>
<input type="text" name="email" value="exemplo@exemplo.com" size="x" onfocus="clearThis(this)" onblur="replace(this)" />
CODE EXPLAINED: When the text box has focus, clear the value.代码解释:当文本框获得焦点时,清除该值。 When text box is not focused AND when the box is blank, replace the value.当文本框未聚焦且框为空白时,替换值。
I hope that works, I have been having the same issue, but then I tried this and it worked for me.我希望这有效,我一直有同样的问题,但后来我尝试了这个,它对我有用。
You don't need to bother with that.你不需要为此烦恼。 Just write随便写
<input type="text" name="email" placeholder="exemplo@exemplo.com" size="30">
replace the value with placeholder用占位符替换值
而不是清除名称文本使用占位符属性,这是一种很好的做法
<input type="text" placeholder="name" name="name">
Try this :试试这个 :
<script type="text/javascript">
function clearThis(target){
if(target.value == "exemplo@exemplo.com")
{
target.value= "";
}
}
</script>
<script type="text/javascript">
function clearThis(target){
if (target.value === "exemplo@exemplo.com") {
target.value= "";
}
}
</script>
<input type="text" name="email" value="exemplo@exemplo.com" size="30" onfocus="clearThis(this)">
Try it out here: http://jsfiddle.net/2K3Vp/在这里试试: http : //jsfiddle.net/2K3Vp/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.