2011-01-25 102 views
1

要继续从上一个问题这里是一个函数用于确定什么元素选择的单选按钮和复选框加载在右手的标志和什么元素通过'blank2.html' 。问题在于它在错误的时间点加载错误的元素。这是代码。JQuery加载函数产生意想不到的结果

function product_analysis_global() { 
$(':checked').each(function() { 
    var alt = $(this).attr('alt'); 
    var title = $(this).attr('title'); 
    if ($('#product_quantity_PRI_' + alt).attr('title') != title) { 
     $('#product_' + alt).load(title); 
     $('#product_quantity_PRI_' + alt).attr('title', title); 
     $('#product_quantity_PRI_' + alt).val($(this).val()); 
    } else if ($('#product_quantity_PRI_' + alt).attr('title') != 'http://www.divethegap.com/update/blank2.html') { 
     $('#product_' + alt).load('http://www.divethegap.com/update/blank2.html'); 
     $('#product_quantity_PRI_' + alt).attr('title', 'http://www.divethegap.com/update/blank2.html'); 
    } else return false ; 
}); 

}

看看它的实现在这里,你会看到正是我的意思。 http://www.divethegap.com/update/diving-trips/adventure-training并点击BEGINNERS加载表单。我相信问题在于$(this).attr('alt')和$(this).attr('title')只收集箱子的一个属性,而不是全部检查箱子。

的附加特征,我想此代码具有处于未选中的方框来治疗禁用复选框或单选按钮,并通过“blank2.html”加载出来

回答

1

$.load打完的异步请求,因此随后的陈述将在之前评估该请求结束(至少在绝大多数情况下)。

你需要做你想要的它success回调中,以确保它是保证发生一次$.load已完成:

// make sure you make the current context available within the callback 
var that = this; 
$('#product_' + alt).load(title, function() { 
     $('#product_quantity_PRI_' + alt).attr('title', title); 
     $('#product_quantity_PRI_' + alt).val($(that).val()); 
}); 
+0

谢谢您的回答。我将这段代码放在一个Ready函数中,假设它会像回调函数那样工作。它没有。对于JQuery,我是一个新手,尽管我非常感谢你的帮助,并且需要关于我应该做什么的更多说明。另外我想问的是,这个预订系统在加载内容时速度很慢。是否有更好的选择? – 2011-01-25 13:09:14