简体   繁体   English

jQuery asp.net,停止回发

[英]jquery asp.net, stopping postback

I have a text box and I want the user to be able to type in the TextBox and when they hit enter, I want jQuery to make a ajax call to a web method. 我有一个文本框,我希望用户能够在TextBox中键入内容,当他们按下Enter键时,我希望jQuery对Web方法进行Ajax调用。
The problem is, when user hits the enter, the method is called but then the page refreshes due to the return. 问题是,当用户按下回车键时,将调用该方法,但是由于返回,页面刷新。 I've tried using return false but with no results. 我试过使用return false但没有结果。

Here is the code: 这是代码:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="X-UA-Compatible" content="IE=7" />  
<script type="text/javascript" src="Scripts/jquery-1.4.1.min.js"></script>
<script language="javascript">
   function serviceCall(getText) {          
      $.ajax({
       type: "POST",
       url: 'ActiveDirectoryAutoFillWebService.asmx/TestMethod',
       data: "{'getId':'+ getText +'}",
       contentType: "application/json; charset=utf-8",
       dataType: "json",
       success: function (msg) {
       $("#divResult").html(msg.d);
       },
       error: function (e) {
       $("#divResult").html("WebSerivce unreachable " + e.name + " " + e.message);
     }
   });  
  //  return false;       This does not work      
}
function searchKeyPress(e) {
   if (typeof e == 'undefined' && window.event) { e = window.event; }
       if (e.keyCode == 13) {              
           serviceCall(e)   
           //  return false;       This does not work         
       }          
 }
 </script>
</head>
<body>

<form id="form1" runat="server">      
    <div id="divResult" style="margin-top: 20px;"></div>
    <asp:TextBox ID="tbSearchName" runat="server" onKeyPress="searchKeyPress(event); "></asp:TextBox>      

</form>
</body>
</html>

If anyone knows how I can accomplish this, please let me know. 如果有人知道我如何做到这一点,请告诉我。

Have a look at the following functions on the event object: 看一下事件对象上的以下功能:

In your case, you can use 您可以使用

e.preventDefault()

This will stop the default action from happening. 这将阻止发生默认操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM