开发者

JQuery Load functions producing unexpected results

To follow on from a previous question here is a function used to determine what elements selected by Radio Buttons and Checkboxes are loaded on the right hand sign and what elements are loaded out via 'blank2.html'. The problem is it is loading the wrong elements in at the wrong points in time. Here is the code.

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 ; 
}); 
开发者_JAVA百科

}

Take a look at its implementation here and you will see precisely what I mean. http://www.divethegap.com/update/diving-trips/adventure-training and the click on BEGINNERS to load in the form.

I believe the problem lies in the $(this).attr('alt') and $(this).attr('title') collecting only one attribute for the box checked not all of them.

An additional feature that I would like this code to have is to treat a disabled checkbox or radio button at an unchecked box and load it out via 'blank2.html'


$.load fires off an asynchronous request, therefore subsequent statements will be evaluated before the request finishes (at least in the vast majority of cases).

You will need to do what you want within its success callback, to ensure that it is guaranteed to happen once $.load has completed:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜