[英]Ajax request showing data again when i click again on button
I am working on laravel and using ajax for getting data from the database and showing it on popup by clicking on the button.我正在研究 laravel 并使用 ajax 从数据库中获取数据并通过单击按钮在弹出窗口中显示它。
The problem is once I click the button it shows me data on the popup.问题是一旦我单击按钮,它就会在弹出窗口中显示我的数据。 but when I close and click again the button it shows popup double data.但是当我关闭并再次单击该按钮时,它会显示弹出双数据。
Ajax Code: Ajax 代码:
<script>
function getMessage(id) {
$.ajax({
url: '/gethandlers/' + id,
type: 'GET',
success: function (resp) {
var trHTML = '';
for (i = 0; i < resp.length; i++) {
trHTML +=
'<tr><td>'
+ resp[i]['name']
+ '</td></tr>';
}
$('#tBody').append(trHTML);
$('#myModal').modal('show');
console.log(resp[0]['name']);
},
});
}
</script>
Route:路线:
Route::get('/gethandlers/{id}', 'ClientsController@gethandlers');
The method in the controller: controller中的方法:
public function gethandlers($id)
{
$data = Multiplehandler::select('handler_id')->where('user_id', $id)->get();
$total_ids = count($data);
$handler_names = [];
foreach ($data as $datas) {
$handler_names [] =Clients::select('name')->where('id', $datas->handler_id)->first();
}
return $handler_names;
}
You are using append but this is not replace the data of your selector, it is just "append".您正在使用 append 但这不是替换选择器的数据,它只是“附加”。
$('#tBody').append(trHTML);
Use利用
$('#tBody').html(trHTML);
will fix your problem.将解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.