在 IE7 中,如何在 jquery .click 事件期间使用 ajax 调用设置全局变量或元素值?
在 IE7 中,当在 .submittable 单击事件中调用此代码时,我必须单击提交按钮两次才能设置 orderCount 全局变量。或者,如果我在隐藏标签上设置属性值,我会得到相同的体验。对于要设置的元素值,我必须单击按钮两次。就像我需要给浏览器更多的时间来处理。有什么建议吗?谢谢你,内特
var orderCount = 0;
$(document).ready(function(){
$('.submittable').click(function(){
setOrderCount()
if ( orderCount == 0 ){
if (validateAcknowledment($(this).attr('id'))){
acknowledgeDay($(this).attr('id'));
}
}
});
});
function setOrderCount(){
// move this up to validate and do a setAtr
var school = $("#school").val();
var orderDate = $("#orderDate").val();
$.ajax({
url: "/fos/inventory/getPreviousWeeklyMenuOrders",
type: "GET",
dataType: 'json',
cache:false,
data: {school: school,
startWeekMonth: Date.parse(orderDate).getMonth(),
startWeekDay: Date.parse(orderDate).getDate(),
startWeekYear: Date.parse(orderDate).getFullYear()
},
success: function(data) {
orderCount = data.orderCount;
if(orderCount > 0){
showErrorMessage("You must Acknowledge prior Weekly Menu Order(s) before acknowledging this week's order", 200, 300);
}
}
});
In IE7, when this code is called in the .submittable click event, I have to click the submit button twice to get the orderCount global variable to be set. Or if I set an attribute value on a hidden tag, I get the same experience. For the element value to be set I have to click the button twice. It's like I need to give the browser more time to process. Any suggetions ? Thank You, Nate
var orderCount = 0;
$(document).ready(function(){
$('.submittable').click(function(){
setOrderCount()
if ( orderCount == 0 ){
if (validateAcknowledment($(this).attr('id'))){
acknowledgeDay($(this).attr('id'));
}
}
});
});
function setOrderCount(){
// move this up to validate and do a setAtr
var school = $("#school").val();
var orderDate = $("#orderDate").val();
$.ajax({
url: "/fos/inventory/getPreviousWeeklyMenuOrders",
type: "GET",
dataType: 'json',
cache:false,
data: {school: school,
startWeekMonth: Date.parse(orderDate).getMonth(),
startWeekDay: Date.parse(orderDate).getDate(),
startWeekYear: Date.parse(orderDate).getFullYear()
},
success: function(data) {
orderCount = data.orderCount;
if(orderCount > 0){
showErrorMessage("You must Acknowledge prior Weekly Menu Order(s) before acknowledging this week's order", 200, 300);
}
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
令人惊奇的是,出现了这么多这样的问题。
默认情况下,AJAX 调用是异步的。您在第 4 行发起调用,然后在异步调用完成之前检查第 5 行的 orderCount。第二次单击时,异步调用现已完成,代码正在执行您认为第一次单击时应该执行的操作。
您需要将 orderCount 检查逻辑移至匿名“成功”回调函数内,以便仅在调用完成时才会执行它。或者使ajax调用同步(jquery选项)
it's amazing how many of these questions come up.
By default the AJAX call is asnychronous. You initiate the call on your 4th line and then check orderCount on the 5th, before your asynchronous call has completed. On your second click your async call has now completed and the code is doing what you think it should be doing on the first click.
You need to move your orderCount checking logic inside your anonymous 'success' callback function, so that it will only be executed when the call is complete. Or make the ajax call synchronous (jquery options)
首先,您需要认识到setOrderCount是一个异步函数。它将启动 ajax 调用,并且该 ajax 调用可能需要一段时间才能处理。 setOrderCount 函数将立即返回,早在 ajax 调用完成之前,因此早在 orderCount 变量被设置之前。
然后,在您的点击处理程序中,您立即检查 orderCount 变量,但它尚未设置。您需要做的就是将此代码从 ajax 调用移动到 success 函数:
因为那是实际设置 orderCount 变量的时候。
First of all, you need to realize that setOrderCount is an asynchronous function. It will launch the ajax call and that ajax call may take awhile to process. The setOrderCount function will return right away, long before the ajax call has completed and thus long before the orderCount variable has been set.
Then, in your click handler, you immediately check the orderCount variable, but it is not set yet. What you need to do is move this code to the success function from the ajax call:
because that's when the orderCount variable is actually set.