对于我的生活,我不明白为什么会这样。
基本上我使用以下代码从数据库中恢复表行。
var getCposInputs = {
'type': 'viewcpos',
'quoteid': '<?php echo $_GET['id']; ?>'
};
$.ajax({
type: 'POST',
url: '/Applications/Controllers/Quotes/ajax-add-sin.php',
data: getCposInputs,
dataType: 'html',
encode: true
}).done(function(data){
//$('body').html(data);
buildcotable += data;
});
因此,您可以看到有一条注释掉的行,当删除时会直接显示详细信息,而不是稍后使用JavaScript中的.html()函数发送的变量。
所以完整的jQuery代码是:
var buildcotable = '';
var buildtrs = $('#formentry15').val();
var coArray = '';
var coArrayNumber = 1;
buildcotable += '<div class="table-responsive">';
buildcotable += '<table class="table table-bordered">';
buildcotable += '<thead>';
buildcotable += '<th class="text-center">Number</th>';
buildcotable += '<th class="text-center">Price</th>';
buildcotable += '<th class="text-center">Installments</th>';
buildcotable += '<th class="text-center">Contact Initials</th>';
buildcotable += '<th class="text-center">Options</th>';
buildcotable += '</thead>';
buildcotable += '<tbody id="jquerypotable">';
//lets do a check and see how many are listed
if(buildtrs != 'TBC'){
var getCposInputs = {
'type': 'viewcpos',
'quoteid': '<?php echo $_GET['id']; ?>'
};
$.ajax({
type: 'POST',
url: '/Applications/Controllers/Quotes/ajax-add-sin.php',
data: getCposInputs,
dataType: 'html',
encode: true
}).done(function(data){
$('body').html(data);
buildcotable += data;
});
} else {
buildcotable += '<tr id="jqueryporow1">';
buildcotable += '<td><input type="hidden" value="No" id="jqueryponumber1" class="form-control">No CPO\'s have been submitted</td>';
buildcotable += '<td><input type="hidden" value="" id="jquerypovalue1" class="form-control"></td>';
buildcotable += '<td class="text-center"><input type="hidden" value="" id="jquerypoinstallments1" class="form-control"></td>';
buildcotable += '<td><input type="hidden" value="" id="jquerypocontactinitials1" class="form-control"></td>';
buildcotable += '<td class="text-center"> </td>';
buildcotable += '</tr>';
}
buildcotable += '</tbody>';
buildcotable += '</table>';
buildcotable += '<p><a href="#" class="btn btn-default btn-block" id="addnewpo">Add New CPO Number</a></p>';
buildcotable += '<p><a href="#" class="btn btn-danger btn-block" id="ubldonepo">Done</a></p>';
buildcotable += '</div>';
$('.ubl-section-7').html(buildcotable);
我知道数据回复正常,因为当我删除$(&#39; body&#39;)。html(数据)的评论时;然后它显示了信息。
但如果试图把它变成一个变量,它就什么都不做。
关于为什么会发生这种情况的任何想法?
由于
答案 0 :(得分:1)
您必须确保在ajax请求检索其数据后生成输出。此外,您的最后一行代码甚至在buildcotable += data;
执行之前就已运行,因为ajax请求尚未准备好(它是异步的)。尝试这样的事情:
var buildcotable_beg = '<table><tr> ...';
var buildcotable_cnt = '';
var buildcotable_end = '...</table>';
var full_bld_tbl = '';
if (buildtrs != 'TBC') {
$.ajax(...).done(function(buildcotable_cnt) {
full_bld_tbl = buildcotable_beg + buildcotable_cnt + buildcotable_end;
$('.ubl-section-7').html(full_bld_tbl);
});
} else {
buildcotable_cnt = '<tr>...</tr>';
full_bld_tbl = buildcotable_beg + buildcotable_cnt + buildcotable_end;
$('.ubl-section-7').html(full_bld_tbl);
}