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

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='默认值'}");
        }
    }
}

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

posted @ 2013-08-05 14:39  看看天上云  阅读(374)  评论(0编辑  收藏  举报