Javascript 如何在laravel刀片中将变量从标记传递到模态
这是我的标签Javascript 如何在laravel刀片中将变量从标记传递到模态,javascript,php,jquery,laravel,Javascript,Php,Jquery,Laravel,这是我的标签 <a data-toggle="modal" data-id="{{$x}}" data-target="#exampleModal" title="آزاد سازی موجودی" class="datatables__button function"><i class="mdi mdi-autorenew"></i>
<a data-toggle="modal" data-id="{{$x}}" data-target="#exampleModal" title="آزاد سازی موجودی" class="datatables__button function"><i class="mdi mdi-autorenew"></i></a>
这是剧本
<script>
$('#exampleModal').on('show.bs.modal', function (event) {
console.log(data)
var button = $(event.relatedTarget)
var recipient = button.data('whatever')
var modal = $(this)
modal.find('.modal-title').text('New message to ' + recipient)
modal.find('.modal-body input').val(recipient)
})
$('exampleModal').on('show.bs.modal',函数(事件){
console.log(数据)
var按钮=$(event.relatedTarget)
var recipient=button.data('whatever')
var modal=$(此)
modal.find('.modal title').text('发送给'+收件人的新邮件)
modal.find('.modal body input').val(收件人)
})
这里是模态,我需要接收变量$x
<form method="get" action="{{route('backend.customers.release-balance',['customer' => $customer->id])}}">
这里是完整的模式
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel"
aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">درخواست آزادسازی موجودی</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form method="get" action="{{route('backend.customers.release-balance',['customer' => $customer->id])}}">
<div class="form-group">
<label for="recipient-name" class="col-form-label">مقدار:</label>
<input name="amount" type="text" class="form-control" id="recipient-name">
</div>
<div class="form-group">
<button type="submit" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Send message</button>
</div>
</form>
</div>
</div>
</div>
درخواست آزادسازی موجودی
&时代;
مقدار:
接近
发送消息
在使用id not函数时,我不知道如何传递变量
我必须向该href发送变量$x
$('a.datatables__button').on('click', function(e) {
e.preventDefault();
var $button = $(this)
var $modal = $('#exampleModal')
let id = $button.data('id')
console.log('id from ahref', id)
$modal.modal('show');
// now you can use the variable id and pass it to the modal
//example
$("input[name='amount']", $modal).val(id)
return false;
})
所以我认为应该是这样的,请尝试一下,并根据您的需要进行修改
您还可以从ahref标签中删除数据切换属性我知道这是您尝试实现的不同解决方案,但我会做一些不同的事情。 首先,我将表单更改为POST方法,并添加隐藏输入,该输入将保存客户ID,也不要忘记@csrf。这意味着您必须更改路由并始终检查后端的数据,因为无论发生什么情况,您都不能信任客户端
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">درخواست آزادسازی موجودی</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form method="post" action="{{route('backend.customers.release-balance')}}">
@csrf
<input type="number" id="input-customer-id" hidden name="customer_id"/>
<div class="form-group">
<label for="recipient-name" class="col-form-label">مقدار:</label>
<input name="amount" type="text" class="form-control" id="recipient-name">
</div>
<div class="form-group">
<button type="submit" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Send message</button>
</div>
</form>
</div>
</div>
现在脚本是
<script>
$('#openModal').on('click', function(){
$('#exampleModal').modal('show');
var button = $(this)
var recipient = button.data('id')
$('#exampleModal').find('.modal-title').text('New message to ' + recipient)
$('#exampleModal').find('.modal-body input').val(recipient)
})
$('openModal')。在('click',function()上{
$('exampleModal').modal('show');
var按钮=$(此)
var recipient=button.data('id')
$('#exampleModal').find('.modal title').text('发送给'+收件人的新邮件)
$('#exampleModal').find('.modal body input').val(收件人)
})
像这样试试
钮扣
因此,您需要在单击时从ahref获取数据id并将其传递到模式窗口,对吗?我认为是这样,但如何传递呢?Route::post(“{customer}/release balance”“,”CustomerController@releaseBalance')->名称('release-balance');roate应该如何改变呢CustomerController@releaseBalance')->名称('release-balance');然后在控制器内部,您将可以访问表单传递的所有输入。您可以添加($request->all())以查看传递给控制器的内容。
<a id="openModal" data-id="{{$x}}" title="آزاد سازی موجودی" class="datatables__button function"><i class="mdi mdi-autorenew"></i></a>
<script>
$('#openModal').on('click', function(){
$('#exampleModal').modal('show');
var button = $(this)
var recipient = button.data('id')
$('#exampleModal').find('.modal-title').text('New message to ' + recipient)
$('#exampleModal').find('.modal-body input').val(recipient)
})
<a data-toggle="modal" data-id="{{$x}}" title="آزاد سازی موجودی" class="datatables__button open_modal function"><i class="mdi mdi-autorenew"></i></a>
<form method="get" class="test_form" action="{{route('backend.customers.release-balance',['customer' => $customer->id])}}">
$('.open_modal ').on('click', function (event) {
let x = $(this).attr('data-id');
var modal = $('#exampleModal');
modal.modal('show');
modal.find('.modal-title').text('New message to ' + recipient);
modal.find('.modal-body input').val(recipient);
$('.test_form').attr('data-id', x);
})