文本框回不回车我说的算

 

我的页面有两个按钮和几个文本框,登录时敲回车键触发"登录"事件,但是搜索时敲回车键也触发"登录"事件,其实应触发"搜索事件"。不管在那个文本框敲回车,总是触发第一按钮的事件。

我给“搜索框”加个keypressdown事件,如果敲的回车键就是执行 “搜索onclick”

function search()

{

if(event.keyCode==13)

 {

document.getElementById('<%=this.ClientID %>_search').onclick();

}

}

代码没错,就是达不到效果。

最后,buttonUseSubmitBehavior 属性设为 falseButtontypesubmit变为button了。之后就可以为文本框指定回车事件。

通常回车键默认触发第一按钮的事件,干吗给回车设个默认事件,真恶心。

                                      

HTML:

<table width="100%" height="66" border="0" cellpadding="0" cellspacing="0">

                <tr>

                    <td width="28%" height="27" align="right">

                        <strong>用户名:</strong></td>

                    <td width="44%" align="center">

                        <input name="textfield" type="text" class="login_1" id="txtUserName" maxlength="16"

                            runat="server" onkeydown="t_Login();" /></td>

                    <td width="28%" rowspan="2">

                       

                       

                       

                        <asp:Button ID="imgLogin" runat="server" Width="46" Height="39" CssClass="t_login" OnClick="imgLogin_Click" UseSubmitBehavior="False" />

                       

                            </td>

                </tr>

</table>

JS:

<script type="text/javascript">

function t_Login()

{

if(event.keyCode==13)

 {

    document.getElementById('<%=this.ClientID %>_imgLogin').onclick();

    }

}

</script>

CS:

    protected void imgLogin_Click(object sender, EventArgs e)

    {

        if (BLL.Format.FormatTextContent(txtUserName.Value) == "")

        {

            JScript.Alert("请输入用户名");

        }

        else

        {

            if (BLL.Format.FormatTextContent(txtPassWord.Value) == "")

            {

                JScript.Alert("请输入密码");

            }

            else

            { Login(); }

        }

}

                                                    

HTML

                <tr>

                    <td width="12%" align="right">

                        &nbsp;</td>

                    <td width="62%" align="left">

                        <asp:TextBox runat="server"   ID="txtkey" Width="170" Text="关键字 职位名称" onmouseover="Clear(this)" />&nbsp;</td>

                    <td width="25%" align="left" style="padding-right: 8px;">

                       

                        <asp:Button ID="imgsearch" runat="server" CssClass="t_s_btn" OnClick="imgsearch_Click" UseSubmitBehavior="False"/>

                           

                    </td>

                </tr>

JS

function search()

{

if(event.keyCode==13)

 {

document.getElementById('<%=this.ClientID %>_imgsearch').onclick();

}

}

CS

    protected void Page_Load(object sender, EventArgs e)

    {

        txtkey.Attributes.Add("onkeydown", "search();");

              

}

    protected void imgsearch_Click(object sender, EventArgs e)

    {

         string strUrl = "list.aspx?area=" + area + "&className=" + className + "&key=" + key + "&city=" + city + "&small=" + small;

        Response.Redirect(strUrl);

 }

登录的文本框是HTML控件,搜索是Asp.net控件。为文本框添加keypressdown事件的方法不同。

posted @ 2008-03-15 02:18  yellowwood  阅读(1320)  评论(0编辑  收藏  举报
Never Give UP