-1

我有一个 asp 页面,顶部有过滤器文本框。这 3 个文本框在其中呈现文本(说明要输入的内容)。我已经添加了代码以在文本框获得焦点时选择所有文本,但现在我需要在某种意义上在控件失去焦点时执行完全相反的操作。此处的目标是使文本框过滤器始终使用默认文本填充,除非用户输入了有效的过滤器值。

基本上

如果 control.text = string.empty 和 control.lostfocus 然后将文本设置回“默认”结束如果

到目前为止,我在页面加载时有此代码...

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
        txtAcctFilter.Attributes.Add("onFocus", "JavaScript:this.select();")
        txtMonthFilter.Attributes.Add("onFocus", "JavaScript:this.select();")
        txtYearFilter.Attributes.Add("onFocus", "JavaScript:this.select();")
        'txtAcctFilter.Attributes.Add("onBlur", )
        'txtMonthFilter.Attributes.Add("onBlur", txtMonthFilter.Text = "Enter Month")
        'txtYearFilter.Attributes.Add("onBlur", txtYearFilter.Text = "Enter Year")
        Populate()
    End If
End Sub

注释掉的 3 行是我的尝试,但我相信它们是在控件上恢复的属性之间的问题。我是 javascript 的菜鸟,所以不确定那里到底发生了什么。关于如何让这个简单的逻辑起作用的任何想法?

4

2 回答 2

0

这将是这样的:

txtMonthFilter.Attributes.Add("onBlur", "javascript: if (this.value == """") {this.value = ""Enter Month""}")
于 2012-11-13T16:52:31.373 回答
0
you can use jquery:

$('input[id$="txtMonthFilter"]').blur( function () {
   if ($(this).val() == '') $(this).val('Enter Month');
});
于 2012-11-13T17:07:06.787 回答