您好,我有以下代码返回多个项目,如下所示:
PHP函数-从while语句返回:该返回结果在具有相同ID(view_contact_activity)但值不同的多行上返回。
echo "
<ul class=\"feeds\">
<li>
<a id=\"view_contact_activity\" href=\"#\" data-toggle=\"modal\">
<div class=\"id\"><input type=\"text\" id=\"this_activity_id\" value=\"$activity_id\"></div>
<div class=\"col1\">
<div class=\"cont\">
<div class=\"cont-col1\">
<div class=\"label label-sm label-success\">
<i class=\"$activity_type_icon\"></i>
</div>
</div>
<div class=\"cont-col2\">
<div class=\"desc\">
$activity_title
</div>
</div>
</div>
</div>
<div class=\"col2\">
<div class=\"dueDate\">
$activity_due_date
</div>
</div>
</a>
</li>
</ul> ";
该脚本触发动作(调用另一个函数,该函数返回JSON对象数组):
$(document).on("click", "#view_contact_activity", function () {
var this_activity_id = $(this).closest('.feeds').find('.id #this_activity_id').val();
$('#view-contact-activity').modal('show')
$('#view-contact-activity').on('show.bs.modal', function() {
$modal = $(this);
$.ajax({
url: '../includes/functions/contact-functions.php',
data: {view_activity_id:this_activity_id},
dataType:'json',
success: function(response){
// Find the elements in the modal
$modal.find('#activity_id').val(response[0].id);
$modal.find('#activity_type').val(response[0].activity_type);
}
});
});
});
编辑-添加JSON结果:
[{"id":"12","activity_type":"meeting","activity_title":"meeting","activity_details":"ds","activity_status":""}]
当我单击(“ view_contact_activity)时,结果返回,因为我单击了上面的一个,或者在模式中有时没有返回。单击几次后,萤火虫中显示更多请求,但返回的值不是被单击的那个。
希望我能说清楚。谢谢
最佳答案
似乎是多个ajax请求的问题。一种可能的解决方案是,如果前一个请求尚未完成,则在发送新请求之前将其终止。
var contactAjax;
$(document).on("click", "#view_contact_activity", function () {
var this_activity_id = $(this).closest('.feeds').find('.id #this_activity_id').val();
$('#view-contact-activity').data('activity-id', this_activity_id).modal('show')
});
//use event delegation if #view-contact-activity is created dynamically
$('#view-contact-activity').on('show.bs.modal', function () {
//create a local variable
var $modal = $(this),
this_activity_id = $modal.data('activity-id');
if (contactAjax && $.isFunction(contactAjax.abort)) {
//abort the previous request
contactAjax.abort();
}
contactAjax = $.ajax({
url: '../includes/functions/contact-functions.php',
data: {
view_activity_id: this_activity_id
},
dataType: 'json',
success: function (response) {
// Find the elements in the modal
$modal.find('#activity_id').val(response[0].id);
$modal.find('#activity_type').val(response[0].activity_type);
}
}).always(function () {
contactAjax = undefined;
});
});