我有一个文本框,我希望用户能够在 TextBox 中输入,当他们按 Enter 键时,我希望 jQuery 对 Web 方法进行 ajax 调用。
问题是,当用户按回车键时,该方法被调用,但随后页面因返回而刷新。我试过使用return false
但没有结果。
这是代码:
<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>
如果有人知道我如何做到这一点,请告诉我。