您好我有以下代码返回多个项目,如下所示: 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)时,结果返回,因为我点击了上面的那个或有时没有返回模态。几次点击后,更多的请求显示在firebug中,但返回的值不是点击的。 希望我说清楚。谢谢
答案 0 :(得分:1)
这看起来像一个问题是多个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;
});
});