jQuery的asp.net,停止回传 [英] jquery asp.net, stopping postback

查看:122
本文介绍了jQuery的asp.net,停止回传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本框,我希望用户能够在文本框中输入,当他们敲回车,我想jQuery来进行Ajax调用一个Web方法。结果
问题是,当用户点击进入,该方法被调用但随后的页面刷新由于回报。我已经尝试过使用返回false ,但没有结果。

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.
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.

这里是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:

  • stopImmediatePropagation
  • stopPropagation
  • preventDefault

在你的情况,你可以使用

In your case, you can use

e.preventDefault()

这将阻止发生的默认操作。

This will stop the default action from happening.

这篇关于jQuery的asp.net,停止回传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆