简体   繁体   English

如何使用javascript清除文本框

[英]How to clear a textbox using javascript

I have a 我有一个

<input type="text" value="A new value">

I need a javascript method to clear the value of the textbox when the focus is on the textbox. 当焦点在文本框上时,我需要一个javascript方法来清除文本框的值。

How can this be achieved? 怎么能实现这一目标?

just get element using 只是使用元素

   function name()
   {  
   document.getElementById('elementid').value = "";
   }

you can call this function on onfocus event of textbox and clear the value 您可以在文本框的onfocus事件上调用此函数并清除该值

It sounds like you're trying to use a "watermark" (a default value that clears itself when the user focuses on the box). 听起来你正试图使用​​“水印”(当用户专注于盒子时,默认值会自动清除)。 Make sure to check the value before clearing it, otherwise you might remove something they have typed in! 确保在清除之前检查该值,否则您可能会删除他们输入的内容! Try this: 试试这个:

<input type="text" value="A new value" onfocus="if(this.value=='A new value') this.value='';">

That will ensure it only clears when the value is "A new value". 这将确保它仅在值为“新值”时清除。

<input type="text" value="A new value" onfocus="javascript: if(this.value == 'A new value'){ this.value = ''; }" onblur="javascript: if(this.value==''){this.value='A new value';}" />

For those coming across this nowadays, this is what the placeholder attribute was made to do. 对于那些现在遇到这种情况的人来说,这就是placeholder属性的作用。 No JS necessary: 没有JS必要:

<input type="text" placeholder="A new value">

简单的一个

onfocus="javascript:this.value=''" onblur="javascript: if(this.value==''){this.value='Search...';}"

使用......

<input type="text" name="yourName" placeholder="A new value" />

just set empty string 只需设置空字符串

    <input type="text" id="textId" value="A new value">
    document.getElementById('textId').value = '';

在文本框的输入标记中给出:

onClick="(this.value='')" 

If using jQuery is acceptable: 如果使用jQuery是可以接受的:

jQuery("#myTextBox").focus( function(){ 
    $(this).val(""); 
} );

使用像这样的onfocus和onblur事件:

<input type="text" name="yourName" value="A new value" onfocus="if (this.value == 'A new value') this.value = '';" onblur="if (this.value == '') this.value = 'A new value';">
<input type="text" name="yourName" value="A new value" onfocus="if (this.value == 'A new value') this.value =='';" onblur="if (this.value=='') alert('Please enter a value');" />

使用javascript的Onfous和onblur文本框

   <input type="text" value="A new value" onfocus="if(this.value=='A new value') this.value='';" onblur="if(this.value=='') this.value='A new value';"/>

For my coffeescript peeps! 因为我的coffeescript偷看!

#disable Delete button until reason is entered
$("#delete_event_button").prop("disabled", true)
$('#event_reason_is_deleted').click ->
    $('#event_reason_is_deleted').val('')
    $("#delete_event_button").prop("disabled", false)
<input type="text" value="A new value" onfocus="this.value='';">

然而,对于那些第二次关注元素例如纠正某些事物的用户来说,这将是非常充实的。

Your HTML code, 你的HTML代码,

jquery code, jquery代码,

$("#textboxID").val(''); $( “#textboxID”)VAL( '')。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM