用户点击进入时阻止页面回发

时间:2013-08-15 19:28:28

标签: javascript asp.net vb.net

我有一个网页表单,当用户点击进入时,页面将被回发。事情是这样的:当用户点击表单时,控件将被聚焦(我的鼠标指针变为当你要点击输入控件时的指针),当点击输入时,我的页面将重新加载。

有没有办法捕获任何键的按键,无论用户点击的位置如何?

4 个答案:

答案 0 :(得分:1)

    //Jquery example:
    //Bind this keypress function to all of the input tags 
$("input").keypress(function (evt) { 
    //Deterime where our character code is coming from within the event 
    var charCode = evt.charCode || evt.keyCode; 
    if (charCode == 13) {
     //Enter key's keycode 
    return false; 
}
     });

答案 1 :(得分:0)

您可以通过捕获 KeyCode 13 在body标签中禁用此功能,但不确定它与浏览器的兼容性。

<body onkeydown = "return (event.keyCode!=13)">

答案 2 :(得分:0)

您可以使用jquery并注册keyup事件,只关注返回键。那么你只需要确保你对给定的这个值做出正确的反应。

$(document).on('keydown',function(e){// logic}

答案 3 :(得分:0)

如果你想在按下enter时阻止按下“点击”按钮,并防止当用户进入输入控件时发生回发/提交,你需要将按钮设置为不回发或我做了什么过去设置了一个按钮,在我的情况下没有设置为我的面板的默认按钮。

<asp:Panel ID="pPanel" runat="server" DefaultButton="bDefault">
   <%-- here are all the input controls and a button that 
    now must be physically clicked to submit--%>
</asp:Panel>

<asp:Button ID="bDefault" runat="server" Text="" CssClass="hidden" OnClientClick="return false;" />

返回false OnClientClick导致按钮不回发。