欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

文本框获取和失去焦点默认值问题

发布时间:2025/3/15 34 豆豆
生活随笔 收集整理的这篇文章主要介绍了 文本框获取和失去焦点默认值问题 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

1. HTML控件<input id="txtName" type="text" value="默认值" />

<script src="script/jquery-1.7.1.min.js" type="text/javascript"></script><script type="text/javascript">$(function () {$("#txtName").focus(function () {if ($(this).val() == "默认值") {$(this).val("");}}).blur(function () {if ($(this).val() == "") {$(this).val("默认值");}})});</script>

 

2.服务器控件.<asp:TextBox> 直接写在客户端上:

<asp:TextBox ID="txtName" runat="server" Text="默认值" OnFocus="javascript:if(this.value=='默认值') {this.value=''}"OnBlur="javascript:if(this.value==''){this.value='默认值'}"></asp:TextBox>


3.服务器控件写到C#服务器端上:

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.Script.Serialization;namespace Web {public partial class Default : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){txtName.Attributes.Add("Value", "默认值");txtName.Attributes.Add("OnFocus", "if(this.value=='默认值') {this.value=''}");txtName.Attributes.Add("OnBlur", "if(this.value==''){this.value='默认值'}");}} }

总结. 单个文本框设置默认值时

转载于:https://www.cnblogs.com/gxmaspx/p/3238154.html

总结

以上是生活随笔为你收集整理的文本框获取和失去焦点默认值问题的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。