页面加载时文本框中的默认文本

发布于 2024-08-06 00:07:16 字数 88 浏览 1 评论 0原文

一般来说,我看到文本框中填写的任何注册表单,例如在此处输入名字或输入姓氏,我怎样才能这样写。是 JavaScript 吗? 你能告诉我如何写这样的内容帮助我吗谢谢

Generally i saw any registration form the textboxes are filled with for example Enter first name or Enter Last name here like that how can i write like that. Is it javascript?
can u tell me how can i write that like that help me thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

秋风の叶未落 2024-08-13 00:07:16

根据您对 phoenix 帖子的评论,您可以尝试如下操作:

    <input type="text" value="Enter your name..." onfocus="CheckText(this, 'Enter your name...');" />
    <script language="javascript">
    function CheckText(e, text){
        if(e.value == text){
            e.value = '';
        }
    }
</script>

Based on your comments to the post by phoenix you could try something like this:

    <input type="text" value="Enter your name..." onfocus="CheckText(this, 'Enter your name...');" />
    <script language="javascript">
    function CheckText(e, text){
        if(e.value == text){
            e.value = '';
        }
    }
</script>
随风而去 2024-08-13 00:07:16

您想要的是 TextBoxWatermark。 AJAX 控制工具包有一个。

另外,这里是自定义 ASP.NET 文本框水印的示例

编辑:下面的示例来自上面引用的链接

控制标记:

<asp:textbox id="txtSimpleSearch" runat="server"></asp:TextBox>

JS 函数:

function WatermarkFocus(txtElem, strWatermark) {
if (txtElem.value == strWatermark) txtElem.value = '';
}
function WatermarkBlur(txtElem, strWatermark) {
if (txtElem.value == '') txtElem.value = strWatermark;
}

代码隐藏:

string strWatermarkSearch = "Search";
txtSimpleSearch.Text = strWatermarkSearch;
txtSimpleSearch.Attributes.Add("onfocus", "WatermarkFocus(this, '" + strWatermarkSearch + "');");
txtSimpleSearch.Attributes.Add("onblur", "WatermarkBlur(this, '" + strWatermarkSearch + "');");

What you want is a TextBoxWatermark. The AJAX Control Toolkit has one.

Also, here is an example of a custom ASP.NET Textbox Watermark.

EDIT: Example below is from link referenced above

Control Markup:

<asp:textbox id="txtSimpleSearch" runat="server"></asp:TextBox>

JS Functions:

function WatermarkFocus(txtElem, strWatermark) {
if (txtElem.value == strWatermark) txtElem.value = '';
}
function WatermarkBlur(txtElem, strWatermark) {
if (txtElem.value == '') txtElem.value = strWatermark;
}

Code Behind:

string strWatermarkSearch = "Search";
txtSimpleSearch.Text = strWatermarkSearch;
txtSimpleSearch.Attributes.Add("onfocus", "WatermarkFocus(this, '" + strWatermarkSearch + "');");
txtSimpleSearch.Attributes.Add("onblur", "WatermarkBlur(this, '" + strWatermarkSearch + "');");
后来的我们 2024-08-13 00:07:16

正如您自己所建议的,您可以使用在 onload 事件上调用的 JavaScript,例如:

<body onload="document.getElementById('firstName').value = 'Enter first name'" ...>

您可以将多个指令分组到一个函数中,然后仅在 onload 上调用该指令。

或者您可以直接在输入中设置值:

<input type="text" id="firstName" name="firstName" value="Enter first name" />

As you suggested yourself, you could use JavaScript called on the onload event, something like:

<body onload="document.getElementById('firstName').value = 'Enter first name'" ...>

You could group multiple instructions in a function and just call that on onload.

Or you could just set the value directly in the input:

<input type="text" id="firstName" name="firstName" value="Enter first name" />
梦里人 2024-08-13 00:07:16

您必须将文本框的 value 属性设置为所需的文本,例如

<input type="text" id="txt1" value="Enter text here" />

当输入接收焦点并且该值是默认值时,然后清除该值。如果用户没有输入任何内容,并且当焦点离开文本框时,会将默认值放回文本框。

另请确保不要使用默认值更新数据库。

You have to set the value property of the textbox to the desired text like

<input type="text" id="txt1" value="Enter text here" />

When input receives focus and is the value is the default one then clear the value. And if the user hasn't entered anything and when focus leaves the textbox put the default values back to the textbox.

Also make sure that you don't update database with the default value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文