我有一个通过ajax输出表的应用程序,表的每一行都有一个用于计划日期的列/输入字段。输入计划日期后,单击该行中的更新按钮,然后触发第二次ajax调用。
我遇到的问题,在选择日期并点击更新按钮时,系统有时可以正常工作,有时也不会..查看我的代码 - 我希望有人可以解释一下。
第一次Ajax通话:
$('.datepicker').datepicker({
todayHighlight:'TRUE',
autoclose: true,
})
$.post('assets/ajax/calldata/newords.php', function(data) {
$('#newords').html(data)
$('.datepicker').datepicker({
todayHighlight:'TRUE',
autoclose: true,
})
});
第一次Ajax通话输出
<table>
<thead>
<tr>
<td>#<td>
<td>Account</td>
<td>Date to Scedule</td>
<td>Action</td>
<tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Test Account</td>
<td>
<input class="datepicker form-control" type="text" id="datetoprint" name="datepicker" data-date-format="dd/mm/yyyy" placeholder="dd/mm/yyyy">
</td>
<td>
<button class="btn btn-default markScheduled" type="button" id="markScheduled" ordid="56">Scheduled</button>
</td>
</tr>
</tbody>
</table>
点击行上的更新后的第二个Ajax调用:
$(document).on('click', 'button.markScheduled', function(e) {
var ordid = $(this).attr('ordid');
var scheduledate = $('input.scheduleDate').val();
$.ajax({
url: "assets/ajax/order.php",
type: "POST",
data: {ordid: ordid,date: scheduledate},
success: function(result){
},
error: function(exception){
alert('Exception:' +exception);
}
});
$.post('assets/ajax/calldata/newords.php', function(data) {
$('#newords').html(data)
$('.datepicker').datepicker({
todayHighlight:'TRUE',
autoclose: true,
})
});
});
我没有得到任何错误,这个系统刷新更新数据库有时会从输入中获取值,有时候不会...有时您必须手动刷新页面才能使条目不再出现在屏幕上..
答案 0 :(得分:0)
在您的问题中遗漏了一些上下文以使其更清晰,但听起来您只应在前请求成功时调用post
请求,例如:
$(document).on('click', 'button.markScheduled', function (e) {
var ordid = $(this).attr('ordid');
var scheduledate = $('input.scheduleDate').val();
$.ajax({
url: "assets/ajax/order.php",
type: "POST",
data: {
ordid: ordid,
date: scheduledate
},
error: function (exception) {
alert('Exception:' + exception);
}
}).done(function () {
$.post('assets/ajax/calldata/newords.php', function (data) {
$('#newords').html(data).find('.datepicker').datepicker({
todayHighlight: 'TRUE',
autoclose: true
});
});
});
});
那说,您最好只使用一个请求更新数据库并处理页面上返回的结果...