如何在焦点中删除文本框中的默认文本? [英] How Do I Remove Default Text In Textbox When It Has Focus?

查看:68
本文介绍了如何在焦点中删除文本框中的默认文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

另外,如果可能的话,如果用户没有在框中输入任何文本,我希望默认文本在失去焦点后保留在文本框中。

Also, if possible, I would like that default text to remain in the text box after it loses focus if the user did not enter any text in the box.

推荐答案

当用户选中文本框时,默认情况下会选择所有文本。

如果用户开始输入其他内容,则删除默认文本并改为记录他们的击键。



不幸的是这种行为如果在开始键入之前使用鼠标单击选择文本框,则似乎不起作用。但是,将其添加到MouseClick事件将使其工作
When a user tabs into a textbox all of the text is selected by default.
If the user starts to type something else then the "default text" is deleted and their keystrokes are recorded instead.

Unfortunately that behaviour doesn't seem to work if you use a mouse-click to select the textbox before you start typing. However adding this to the MouseClick event will make it work
private void textBox1_MouseClick(object sender, MouseEventArgs e)
{
    ((TextBox)sender).SelectAll();
}



如果用户离开控件(使用鼠标或Tab键)而不输入任何内容,则默认文本保留在原位,否则当他们开始输入时删除整个文本


If the user moves away from the control (either by using the mouse or tab key) without typing anything in then the default text is left in place, otherwise the entire text is removed when they start typing


试试这个:



Just try this:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>




<script type="text/javascript">


(document).ready(function(){

/ / 1.帮助字段
(document).ready(function () { //1. helping fields


这篇关于如何在焦点中删除文本框中的默认文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆