1、输入框得到焦点,清空输入框

很多页面例如搜索框等,需要当鼠标焦点在输入框中时,清空输入框中内容,这个非常容易实现,请看下面代码:
 <asp:textbox id="txb_KeyWord" MaxLength="20" Runat="server" Width="100px" EnableViewState="False" onFocus="this.value='';">请输入关键字</asp:textbox>
仅仅只需要在该控件代码中加入一个属性就可以,为:onFocus=this.value='';注意是两个单引号,在前面完整的代码中this.value='';又包括在双引号中。

2、回车键提交表单
ASP.NET页面本身没有办法自己实现,这里使用客户端的javascript脚本检查用户的每一次按键,如果为回车,那么提交表单,注意程序中两个颜色不同的控件名称必须与你的ASPX页面中的控件的名称完全一致,其中Form1为页面表单的ID,btn_Select是需要回车触发的服务器按钮的名称。
<script language="javascript">
    function KeyDownHandler()
    {
        // process only the Enter key
        if (event.keyCode == 13)
        {
            // cancel the default submit
            event.returnValue=false;
            event.cancel = true;
            // submit the form by programmatically clicking the specified button
            Form1.btn_Select.click();
        }
    }
</script>

下面是几篇相关文章
Setting the default Button for a TextBox in ASP.NET
Submitting default buttons when the user presses the Enter key - finally!

posted on 2004-06-10 16:33  zhumk  阅读(1768)  评论(1编辑  收藏  举报