【问题标题】:How to pass a value in href into modal window in laravel?如何将href中的值传递给laravel的模态窗口?
【发布时间】:2021-12-08 13:13:16
【问题描述】:

我在 laravel 中显示了下表中的列表:

       <table class="table table-striped">
                    <thead>
                    <tr>
                        <th>ID</th>
                        <th>Name</th>
                        <th></th>
                    </tr>
                    </thead>
                    <tbody>

                    @php $sln = 1 @endphp

                    @foreach ($roles as $role)
                        <tr>
                            <td>{{ $sln++ }}</td>
                            <td>{{ $role->name }}</td>
                            <td><a href="" class="btn addbtn editrole" data-bs-toggle="modal" data-bs-target="#role-edit-modal">Edit</a></td>
                        </tr>
                    @endforeach
                    </tbody>
                </table>

点击下面给出的&lt;a&gt; Edit,我在引导程序中还有一个模态表单。

<div class="modal fade" id="role-edit-modal" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Edit Role</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <div class="formbox">
                    <form action="" method="post" id="role-form">
                        <div class="form-group">
                            <label for="roleName">Role Name</label>
                            <input type="text" name="name" id="role-name" class="form-control" maxlength="50" value="{{ old('email', $role->name) }}">
                            <span id="role-error" class="text-danger"></span>
                        </div>
                        <div class="btngroup">
                            <button type="submit" class="btn btn-primary">Submit</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

我想从to this modal file and display the value in$role->name`传递$role-&gt;id的值`

如何在点击 jQuery 中的编辑按钮并触发模态窗口时传递 id 的值。

【问题讨论】:

    标签: laravel


    【解决方案1】:

    你需要为此使用 Jquery 或 JS

    <td>
        <a href="#" data-id="{{$role->id}}" class="btn addbtn editrole">Edit</a>
    </td>
    
    $('.addbtn').click(function(e){
        e.preventDefault();
        let id = $(this).attr('data-id');
        $('#role-edit-modal').modal('show');
        alert(id);
    });
    

    【讨论】:

      【解决方案2】:

      你可以试试这样的:

      查看:

      <td><a href="" class="btn addbtn editrole" data-bs-toggle="modal" data-bs-target="#role-edit-modal" onclick="getRoleDetails('{{$role->id}}', '{{$role->name}}');" >Edit</a></td>
      

      JS:

      function getRoleDetails (roleId, roleName) {
      
         // If you want to display the role ID
      
         document.getElementById('role-name').value = roleId;
      
      
         // If you want to display the role name
      
         document.getElementById('role-name').value = roleName;
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-14
        • 1970-01-01
        • 2016-12-21
        • 1970-01-01
        • 2021-10-19
        • 1970-01-01
        相关资源
        最近更新 更多