我首先列出细节然后将所有内容放在一起,以确保我彻底。
我有一个脚本,我单击一个表格行,在左侧面板中显示该行的详细信息。在该面板中,还有一个名为Approve Attachment的按钮,当单击时,将执行AJAX POST请求以更改记录(通过更改字段),并通过删除该特定行并更改顶部的计数来更新单击的原始表
具体脚本:
$(document).on("click", ".approveAttachment", function() {
$.ajax({
type: 'POST',
url: '/equipment/attachments/details/approve',
data: {
attachmentID: $(this).data('id'),
approvalID: 1,
_token: $('input[name=_token]').val(),
dataType: 'json',
},
success: function(data) {
if (data !== undefined) {
var id = data.details.id;
var table = $('#unapprovedAttachments');
table.find('#' + id).hide();
$('#attachmentDetails').html('');
var count = $('#attachmentCount').html();
var newCount = count - 1;
$('#attachmentCount').html(newCount);
} else {
console.log('failed');
console.log(data);
}
},
error: function(data) {
console.log('error');
console.log(data);
}
});
});
我的问题出现了,我每次点击批准按钮时都会发现,比如说下一条记录或下一条记录或之后的第n个数字,请求数量增加1.因此第一条记录仅批准发布一次,但是下一次被批准发布两次并且在此之后继续增加1次。
我完全不确定为什么以及如何做到这一点,并且非常恶化。以下是整个页面"所以我假设如果上面的特定脚本没有引起这种情况,下面代码中的内容是:
<div class="col-md-3">
<div class="box box-default box-solid">
<div class="box-header with-border">
<h3 class="box-title">Attachments Requiring Review (<span id="attachmentCount">{{$attachments->count()}}</span>)</h3>
<!-- /.box-tools -->
</div>
<!-- /.box-header -->
<div class="box-body">
{{ csrf_field() }}
<div style="max-height: 100vh; overflow-y:scroll;">
<table class="table table-striped table-hover" id="unapprovedAttachments">
<tbody>
@foreach($attachments as $attachment)
<tr style="cursor: pointer" class="attachmentView" id="{{$attachment->id}}">
<td>{{$attachment->id}}<br>
@if($attachment->unitID)
@if(empty($attachment->equipment->unit_id))
No Unit ID
@else
{{$attachment->equipment->unit_id}}
@endif
@endif
</td>
<td>{{$attachment->attachmentCategory}}<br>@if($attachment->date){{$attachment->date->format('F j, Y')}}@endif</td>
<td>{{$attachment->vendor}}</td>
<td><i class="fa fa-fw fa-arrow-right"></i></td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
<!-- /.box-body -->
</div>
</div>
<div class="col-md-9" id="attachmentDetails">
</div>
<script>
$(document).on("click", ".attachmentView", function(){
$.ajax({
type:'GET',
url: '/equipment/attachments/details/api',
data: {
attachmentID: $(this).attr('id'),
_token: $('input[name=_token]').val(),
dataType: 'json',
},
success: function(data) {
if(data !== undefined) {
$('#attachmentDetails').html('<div class="col-md-8"><div class="box box-default box-solid"><div class="box-header with-border"><h3 class="box-title">Attachment View</h3> </div><div class="box-body"><div id="viewer" style="height: 600px; width: 100%"> <object data="'+data.url+'" type="application/pdf" width="100%" height="100%"><iframe src="'+data.url+'" style="border: none;" width="100%" height="100%">This browser does not support PDFs. Please download the PDF to view it: <a href="'+data.url+'">Download PDF</a></iframe></object></div></div></div></div><div class="col-md-4"><button class="btn btn-primary approveAttachment" data-id="'+data.details.id+'">Approve Attachment</button><div class="box box-default box-solid"><div class="box-header with-border"><h3 class="box-title">Details</h3></div><div class="box-body"><table class="table table-striped table-condensed"><tbody><tr><td><strong>Category</strong></td><td>'+data.details.attachmentCategory+'</td></tr><tr><td><strong>Unit ID</strong></td><td>'+data.equipment.unit_id+'</td></tr><tr><td><strong>Date</strong></td><td>'+data.details.date+'</td></tr><tr><td><strong>Vendor</strong></td><td>'+data.details.vendor+'</td></tr><tr><td><strong>Service ID</strong></td><td>'+data.details.serviceID+'</td></tr><tr><td><strong>Odometer</strong></td><td>'+data.details.odometer+'</td></tr><tr><td><strong>Amount</strong></td><td>'+data.details.amount+'</td></tr><tr><td><strong>Title</strong></td><td>'+data.details.title+'</td></tr><tr><td><strong>Emissions Passed</strong></td><td>'+data.details.emissionsPassed+'</td></tr><tr><td><strong>Emissions Verification No.</strong></td><td>'+data.details.emissionsVerificationNo+'</td></tr><tr><td><strong>File Path</strong></td><td><input class="form-control" name="attachmentPath" value="'+data.details.attachment+'"></td></tr></tbody></table><button class="btn btn-primary updateAttachment" data-id="'+data.details.id+'">Update Attachment</button></div> </div><div class="box box-default box-solid"><div class="box-header with-border"><h3 class="box-title">Maintenance Details</h3></div><div class="box-body"><table class="table table-striped table-condensed" id="attachmentMaintenanceDetails"><tbody></tbody></table></div></div></div>');
$.each(data.maintenance, function(i,val) {
$('<tr>').append(
$('<td>').html(val.maintenanceCategory),
$('<td>').html(val.description)).appendTo('#attachmentMaintenanceDetails');
});
$(document).on("click", ".updateAttachment", function(){
$.ajax({
type:'POST',
url: '/equipment/attachments/details/update',
data: {
attachmentID: $(this).data('id'),
attachmentPath: $('input[name=attachmentPath]').val(),
_token: $('input[name=_token]').val(),
dataType: 'json',
},
success: function(data) {
if(data !== undefined) {
$('#viewer').html('<object data="'+data.url+'" type="application/pdf" width="100%" height="100%"><iframe src="'+data.url+'" style="border: none;" width="100%" height="100%">This browser does not support PDFs. Please download the PDF to view it: <a href="'+data.url+'">Download PDF</a></iframe></object>');
}else{
console.log('failed');
console.log(data);
}
},
error: function(data) {
console.log('error');
console.log(data);
}
});
});
$(document).on("click", ".approveAttachment", function(){
$.ajax({
type:'POST',
url: '/equipment/attachments/details/approve',
data: {
attachmentID: $(this).data('id'),
approvalID: 1,
_token: $('input[name=_token]').val(),
dataType: 'json',
},
success: function(data) {
if(data !== undefined) {
var id = data.details.id;
var table = $('#unapprovedAttachments');
table.find('#'+id).hide();
$('#attachmentDetails').html('');
var count = $('#attachmentCount').html();
var newCount = count - 1;
$('#attachmentCount').html(newCount);
}else{
console.log('failed');
console.log(data);
}
},
error: function(data) {
console.log('error');
console.log(data);
}
});
});
}else{
console.log('failed');
console.log(data);
}
},
error: function(data) {
console.log('error');
console.log(data);
}
});
});
</script>
答案 0 :(得分:2)
原因是您在.updateAttachment
按钮的AJAX成功函数中添加了.approveAttachment
和.attachmentView
的点击处理程序。因此,每次单击该按钮时,都会为其他按钮添加其他单击处理程序,使其多次运行。
您应该将这些点击处理程序放在顶层。
$(document).on("click", ".attachmentView", function() {
$.ajax({
type: 'GET',
url: '/equipment/attachments/details/api',
data: {
attachmentID: $(this).attr('id'),
_token: $('input[name=_token]').val(),
dataType: 'json',
},
success: function(data) {
if (data !== undefined) {
$('#attachmentDetails').html('<div class="col-md-8"><div class="box box-default box-solid"><div class="box-header with-border"><h3 class="box-title">Attachment View</h3> </div><div class="box-body"><div id="viewer" style="height: 600px; width: 100%"> <object data="' + data.url + '" type="application/pdf" width="100%" height="100%"><iframe src="' + data.url + '" style="border: none;" width="100%" height="100%">This browser does not support PDFs. Please download the PDF to view it: <a href="' + data.url + '">Download PDF</a></iframe></object></div></div></div></div><div class="col-md-4"><button class="btn btn-primary approveAttachment" data-id="' + data.details.id + '">Approve Attachment</button><div class="box box-default box-solid"><div class="box-header with-border"><h3 class="box-title">Details</h3></div><div class="box-body"><table class="table table-striped table-condensed"><tbody><tr><td><strong>Category</strong></td><td>' + data.details.attachmentCategory + '</td></tr><tr><td><strong>Unit ID</strong></td><td>' + data.equipment.unit_id + '</td></tr><tr><td><strong>Date</strong></td><td>' + data.details.date + '</td></tr><tr><td><strong>Vendor</strong></td><td>' + data.details.vendor + '</td></tr><tr><td><strong>Service ID</strong></td><td>' + data.details.serviceID + '</td></tr><tr><td><strong>Odometer</strong></td><td>' + data.details.odometer + '</td></tr><tr><td><strong>Amount</strong></td><td>' + data.details.amount + '</td></tr><tr><td><strong>Title</strong></td><td>' + data.details.title + '</td></tr><tr><td><strong>Emissions Passed</strong></td><td>' + data.details.emissionsPassed + '</td></tr><tr><td><strong>Emissions Verification No.</strong></td><td>' + data.details.emissionsVerificationNo + '</td></tr><tr><td><strong>File Path</strong></td><td><input class="form-control" name="attachmentPath" value="' + data.details.attachment + '"></td></tr></tbody></table><button class="btn btn-primary updateAttachment" data-id="' + data.details.id + '">Update Attachment</button></div> </div><div class="box box-default box-solid"><div class="box-header with-border"><h3 class="box-title">Maintenance Details</h3></div><div class="box-body"><table class="table table-striped table-condensed" id="attachmentMaintenanceDetails"><tbody></tbody></table></div></div></div>');
$.each(data.maintenance, function(i, val) {
$('<tr>').append(
$('<td>').html(val.maintenanceCategory),
$('<td>').html(val.description)).appendTo('#attachmentMaintenanceDetails');
});
} else {
console.log('failed');
console.log(data);
}
},
error: function(data) {
console.log('error');
console.log(data);
}
});
});
$(document).on("click", ".updateAttachment", function() {
$.ajax({
type: 'POST',
url: '/equipment/attachments/details/update',
data: {
attachmentID: $(this).data('id'),
attachmentPath: $('input[name=attachmentPath]').val(),
_token: $('input[name=_token]').val(),
dataType: 'json',
},
success: function(data) {
if (data !== undefined) {
$('#viewer').html('<object data="' + data.url + '" type="application/pdf" width="100%" height="100%"><iframe src="' + data.url + '" style="border: none;" width="100%" height="100%">This browser does not support PDFs. Please download the PDF to view it: <a href="' + data.url + '">Download PDF</a></iframe></object>');
} else {
console.log('failed');
console.log(data);
}
},
error: function(data) {
console.log('error');
console.log(data);
}
});
});
$(document).on("click", ".approveAttachment", function() {
$.ajax({
type: 'POST',
url: '/equipment/attachments/details/approve',
data: {
attachmentID: $(this).data('id'),
approvalID: 1,
_token: $('input[name=_token]').val(),
dataType: 'json',
},
success: function(data) {
if (data !== undefined) {
var id = data.details.id;
var table = $('#unapprovedAttachments');
table.find('#' + id).hide();
$('#attachmentDetails').html('');
var count = $('#attachmentCount').html();
var newCount = count - 1;
$('#attachmentCount').html(newCount);
} else {
console.log('failed');
console.log(data);
}
},
error: function(data) {
console.log('error');
console.log(data);
}
});
});
答案 1 :(得分:1)
您正在为.approveAttachment
的单击处理程序中的.attachmentView
元素添加单击处理程序,该处理程序是父元素。因此,每次单击.approveAttachment
元素时,.attachmentView
的处理程序将触发并为.approveAttachment
添加另一个单击处理程序。您需要确保批准的单击处理程序仅添加一次。尝试取消嵌套点击处理程序,如下所示:
$(document).on("click", ".attachmentView", function(){
//...
});
$(document).on("click", ".updateAttachment", function(){
//...
});
$(document).on("click", ".approveAttachment", function(){
//...
});