Ajax / jQuery时序问题

时间:2012-03-04 16:29:47

标签: ajax jquery timing

我有一个click函数,它执行jQuery / Ajax $ .post,以便在单击span时从Web服务获取数据。当在click函数上设置Firebug断点时,一切都按预期工作(一些新的表tr被附加到表中)。如果没有设置断点,则单击跨度时不会发生任何操作。 Firebug没有显示任何错误。我从其他stackoverflow问题中假设这是一个时间问题,但我不知道该怎么做。我尝试从$ .post更改为$ .ajax并将async设置为false,但这并没有解决它。这是点击处理程序的代码:

$('.rating_config').click(function(event){
    event.preventDefault();
    event.stopPropagation();
    var that = $(this);

    // calculate the name of the module based on the classes of the parent <tr>
    var mytrclasses = $(this).parents('tr').attr('class');
    var modulestart = mytrclasses.indexOf('module-');
    var start = mytrclasses.indexOf('-', modulestart) + 1;
    var stop = mytrclasses.indexOf(' ', start);
    var mymodule = mytrclasses.substring(start, stop);
    mymodule = mymodule.replace(/ /g, '+');
    mymodule = mymodule.replace(/_/g, '+');
    mymodule = encodeURI(mymodule);

    // calculate the name of the property based on the classes of the parent <tr>
    var propertystart = mytrclasses.indexOf('property-');
    var propstart = mytrclasses.indexOf('-', propertystart) + 1;
    var propstop = mytrclasses.indexOf(' ', propstart);
    var myproperty = mytrclasses.substring(propstart, propstop);
    myproperty = myproperty.replace(/ /g, '+');
    myproperty = myproperty.replace(/_/g, '+');
    myproperty = encodeURI(myproperty);
    var parentspanid = $(this).attr('id');

    // Remove the comparison rows if they are already present, otherwise generate them
    if ($('.comparison_' + parentspanid).length != 0) {
        $('.comparison_' + parentspanid).remove();
    } else {
        $.post('http://localhost/LearnPHP/webservice.php?user=user-0&q=comparison&level=property&module=' + mymodule + '&version_id=1.0&property=' + myproperty + '&format=xml', function(data) {
            var data = $.xml2json(data);

            for (var propnum in data.configuration.modules.module.properties.property) {
                var prop = data.configuration.modules.module.properties.property[propnum];
                console.log(JSON.stringify(prop));
                prop.mod_or_config = 'config';
                var item_id = mymodule + '?' + prop.property_name + '?' + prop.version_id + '?' + prop.value;
                item_id = convertId(item_id);
                prop.id = item_id;
                //alert('prop.conformity = ' + prop.conformity);
                // genRow(row, module, comparison, comparison_parentspanid)
                var rowstring = genRow(prop, mymodule, true, parentspanid);
                console.log('back from genRow. rowstring = ' + rowstring);
                $(that).closest('tr').after(rowstring);
                //$('tr#node-' + data[row].id + ' span#rating' + row.id).css('background', '-moz-linear-gradient(left, #ff0000 0%, #ff0000 ' + data[row].conformity + '%, #00ff00 ' + 100 - data[row].conformity + '%, #00ff00 100%');
                var conformity_color = getConformityColor(prop.conformity);
                $('tr#comparison_module_' + mymodule + '_setting_' + prop.id + ' span#module_' + mymodule + '_rating' + prop.id).css({'background':'-moz-linear-gradient(left, ' + conformity_color + ' 0%, ' + conformity_color + ' ' + prop.conformity + '%, #fffff0 ' + prop.conformity + '%, #fffff0 100%)'});
                //$('tr#comparison-' + data[row].id + ' span#rating' + data[row].id).css('background','-webkit-linear-gradient(left,  #00ff00 0%, #00ff00 ' + data[row].conformity + '%, #ff0000 ' + (100 - (data[row].conformity + 2)) + '%, #ff0000 100%)');

            }
        });
        // Hide the Fix by mod column
        hideFixedByModCol();

        $('tr.comparison_' + parentspanid).each(function(i){
                if (i % 2 == 0) {
                    $(that).addClass('comparison_even');
                } else {
                    $(that).addClass('comparison_odd');
                }
            });
    }

  });

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

我怀疑你的数据回归不正确。将您的代码包含在try {} catch {}内的中断处,以查看生成的错误。此外,最好将错误处理添加到您的ajax请求中。