简体   繁体   English

如何在 laravel 刀片中将变量从标签传递到模态

[英]how to pass variable from tag to modal in laravel blade

this is my tag这是我的标签

    <a data-toggle="modal" data-id="{{$x}}" data-target="#exampleModal" title="آزاد سازی موجودی" class="datatables__button function"><i class="mdi mdi-autorenew"></i></a>

this is script这是脚本

<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)
    })

and here is modal where I need to recive variable $x这是模态,我需要接收变量 $x

                <form method="get" action="{{route('backend.customers.release-balance',['customer' => $customer->id])}}">

here is complete modal这是完整的模态

<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">&times;</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>

I don't know how to pass variable when in using id not function I have to send variable $x to that href我不知道如何在使用 id 不是 function 时传递变量我必须将变量 $x 发送到该 href

$('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;

})

so I think should be something like this, please try it and modify it by your needs Also you can remove data-toggle attribute from ahref tag所以我认为应该是这样的,请尝试并根据您的需要进行修改您还可以从 ahref 标签中删除 data-toggle 属性

I know it's different solution that you are trying to achieve but I would do it somewhat different.我知道您要实现的是不同的解决方案,但我会做一些不同的事情。 First I would change form to POST method and add hidden input which will hold the customer ID, also don't forget @csrf.首先,我会将表单更改为 POST 方法并添加将保存客户 ID 的隐藏输入,也不要忘记@csrf。 This means you have to change your route and always check the data on backend as you can't trust client side no matter what.这意味着您必须更改路由并始终检查后端的数据,因为无论如何您都不能信任客户端。

<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">&times;</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>

Then for javascript part when opening the modal you can use event show as you are but you should inject the data value into the input.然后对于 javascript 部分,当打开模式时,您可以按原样使用事件显示,但您应该将数据值注入输入。 For example I've added id attribute "input-customer-id"例如我添加了 id 属性“input-customer-id”

   $('#exampleModal').on('show.bs.modal', function (event) {
    var button = $(event.relatedTarget);
   
    var recipient = button.data('id') // Target data-id
    console.log(recipient); // Here you can see the data-id value from a element
    var modal = $(this)
    modal.find('.modal-title').text('New message to ' + recipient)
    modal.find('.modal-body input').val(recipient)
    modal.find('#input-customer-id').val(recipient); // set input value
})
<a id="openModal"  data-id="{{$x}}"  title="آزاد سازی موجودی" class="datatables__button function"><i class="mdi mdi-autorenew"></i></a>

Now script is现在脚本是

<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)
})

Try like this Button试试这个按钮

 <a data-toggle="modal" data-id="{{$x}}"  title="آزاد سازی موجودی" class="datatables__button open_modal function"><i class="mdi mdi-autorenew"></i></a>

Form tag like this像这样的表单标签

<form method="get" class="test_form" action="{{route('backend.customers.release-balance',['customer' => $customer->id])}}">

And jquery script like this和 jquery 这样的脚本

$('.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);
    })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM