jQuery 停止按钮回发
我需要一些有关 jQuery 和停止 asp.net 按钮回发的帮助。如果你看下面的代码,我定义了一个名为 err 的变量,并将其值设置为 0。然后我让 jquery 从 Web 服务获取一个字符串,它实际上是一个错误字符串。如果该字符串的长度为 0,则将 err 设置为 1,否则将 err 设置为 2,因为我们将返回业务规则错误。
当我调试应用程序时,似乎 err 等于 0,而不是 1 或 2。似乎 Web 服务在我应该停止回发的最后一个 if 语句之后获取其值。
任何帮助将不胜感激。谢谢。
$(document).ready(function () {
$('#<%=Button1.ClientID %>').click(function (e) {
//Find out the hour and time of the current request
var myString = $('#<%=DropDownList1.ClientID %>').val();
var myResult = myString.split(":");
var myHour = myResult[0];
var myTime = myResult[1];
var xDate = $("*[id$='hiddenDate']").val();
var err = 0;
//Check to see if the end time of the event somehow falls during a time when another event is occuring.
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "StudiesWebService.asmx/CheckEndTime",
data: "{'UserName':'" + "jacksonm" + "','xDate':'" + xDate + "','xHour':'" + myHour + "','xMinutes':'" + myTime + "'}",
dataType: "json",
success: function (data) {
var myReturnedString = data.d;
//if the returned string length is greater than 0, then set err = 2
if (myReturnedString.length = 0) {
err = 1;
}
else {
err = 2;
}
$('.result').html(myReturnedString);
},
error: function (e) {
$('.result').html("An Error occured checking the validity of this event." + e);
err = 2;
}
});
//stop the postback here depending on what err equals.
if (err == 2) {
return false;
}
else {
return true;
}
});
});
<asp:Button ID="Button1" runat="server" Text="Submit Time" class="CLOSE" onclick="Button1_Click" />
I need some help with jQuery and stopping an asp.net button postback. If you look at the code below, I defined a variable named err and gave it a value of 0. Then I have jquery get a string from a web service, it's actually an error string. If the length of this string is 0, then set err equals to 1, otherwise set err equals to 2 because we're getting back a business rule error.
When I debug the app, it seems that err equals 0, and never 1 or 2. It also seems that the web service gets its value after my last if statement where i'm supposed to stop the postback.
Any help would be appreciated. Thanks.
$(document).ready(function () {
$('#<%=Button1.ClientID %>').click(function (e) {
//Find out the hour and time of the current request
var myString = $('#<%=DropDownList1.ClientID %>').val();
var myResult = myString.split(":");
var myHour = myResult[0];
var myTime = myResult[1];
var xDate = $("*[id$='hiddenDate']").val();
var err = 0;
//Check to see if the end time of the event somehow falls during a time when another event is occuring.
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "StudiesWebService.asmx/CheckEndTime",
data: "{'UserName':'" + "jacksonm" + "','xDate':'" + xDate + "','xHour':'" + myHour + "','xMinutes':'" + myTime + "'}",
dataType: "json",
success: function (data) {
var myReturnedString = data.d;
//if the returned string length is greater than 0, then set err = 2
if (myReturnedString.length = 0) {
err = 1;
}
else {
err = 2;
}
$('.result').html(myReturnedString);
},
error: function (e) {
$('.result').html("An Error occured checking the validity of this event." + e);
err = 2;
}
});
//stop the postback here depending on what err equals.
if (err == 2) {
return false;
}
else {
return true;
}
});
});
<asp:Button ID="Button1" runat="server" Text="Submit Time" class="CLOSE" onclick="Button1_Click" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除非您的 Ajax Web 服务调用实际上是同步的,否则您的代码执行顺序与您预期的不同。向您的成功回调、错误回调以及此注释之前添加一个警报
//根据 err 等于的内容在此处停止回发。
最后一个警报应该首先发生,因为 Web 服务调用不会' t 返回并执行,直到当前函数完成。在 Ajax 调用中,添加此选项
async: false
:Unless your Ajax web service call is actually synchronous then the order your code is executing is not as you expect. Add an alert to your success callback, your error callback, and right before this comment
//stop the postback here depending on what err equals.
The last alert should happen first, since the web service call won't return and be executed until after the current function completes.In your Ajax call, add this option
async: false
:您将 myReturnedString.length var 设置为 0!
尝试将行更改
为
编辑:
要停止回发,您可以尝试:
You're setting your myReturnedString.length var to 0!
try changing the line
to
EDIT:
to stop the postback you can try: