php - 如何使用 ajax 将正确的 ID 发送到控制器?
问题描述
如何将代码添加到输入按钮,以便将 Id 的值发送到控制器?
我有八张图片。然后我点击116的id,打开这个图片。
当我点击最大的照片时,为我打开一个引导模式。
这个动作标签表单被写入这个 ID 120
,而我选择了 ID 116
。
http://localhost:8000/admin/products/4/galleries/120
@section('content')
<div class="col-md-10 p-5 pt-2">
<form method="post" action="{{ route('products.galleries.store', $product->id) }}" enctype="multipart/form-data" class="dropzone" id="dropzone">
@csrf
</form>
<hr>
<div class="col-md-6 mx-auto">
<div class="row">
<div class="mx-auto" id="showImage">
<img src="{{ asset("storage/{$gallery->image}") }}" class="img-fluid cursor-pointer" onclick="deleteImage('{{ $gallery->id }}', '{{ route('products.galleries.delete', [$product->id, $gallery->id]) }}')">
</div>
</div>
<div class="row">
@foreach($galleries as $gallery)
<div class="col-md-2">
<img src="{{ asset("storage/{$gallery->image}") }}" class="img-fluid cursor-pointer" onclick="showImage('{{ asset("storage/{$gallery->image}") }}', '{{ $gallery->id }}')"><br><div class="text-center text-danger">{{ $gallery->id }}</div>
</div>
@endforeach
</div>
</div>
</div>
<div class="modal fade" id="delete" data-backdrop="static" data-keyboard="false" tabindex="-1" aria-labelledby="delete" aria-hidden="true">
<div class="modal-dialog modal-sm">
<form action="{{ route('products.galleries.delete', [$product->id, $gallery->id]) }}" method="post">
@csrf
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">This action is not reversible.</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
Are you sure you want to delete the image?
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-danger">Delete</button>
</div>
</div>
</form>
</div>
</div>
@endsection
@push('script')
<script src="{{ asset('themes/js/dropzone.min.js') }}"></script>
<script>
function showImage(url, id) {
let route = "'"+'{{ route('products.galleries.delete', [$product->id, $gallery->id]) }}'+"'";
let image = '<img src='+url+' onclick="deleteImage('+id+','+route+')" class="img-fluid cursor-pointer" />';
$('#showImage').html(image);
}
function deleteImage(id, url) {
$('#delete').modal(id, url);
}
</script>
@endpush
网页.php
Route::post('products/{product}/galleries/{gallery}', 'GalleryController@delete')->name('products.galleries.delete');
解决方案
推荐阅读
- r - 使用 dplyr::mutate 计算“平均销售份额”
- autodesk-forge - 是否可以从 HDRI 文件添加/创建自定义环境
- swiftui - 如何在 SwiftUI 中创建像单元格一样对齐的文本单元格行
- reactjs - React, Typescript, Axios consume response?
- wpf - 如何让 Path 对象停止裁剪到其边界?
- java - 如何在 Java 中比较 2 个不同的 hashmap
- react-native - 如何安全地保存 redux-persist-transform-encrypt 密钥?
- python - SQLAlchemy - 会话返回值,游标不
- javascript - 渲染后如何在反应中获得状态
- flutter - 如何更改从颤动列表中生成的小部件的状态