javascript - 将变量 id 传递到 Modal

标签 javascript jquery

我使用此代码将 foreach 循环中的 ID 列表加载到模态中。我遇到的问题是,仅填充第一个 ID,而不填充单击元素的 ID。在约会 ID 上设置的正确变量是什么,以便加载相关 ID。

$('#appointmentModal').on('show.bs.modal', function(e) {
        //get data-id attribute of the clicked element
        var appointmentID = ($('[data-id]').val()); 
        //populate the textbox
        $(e.currentTarget).find('input[name="appointmentID"]').val(appointmentID);
        });

按钮

<button type="button" class="btn btn-sm btn-teal " data-toggle="modal" data-target="#appointmentModal" data-appointment="{{ $item->appointmentID }}" data-id="{{ $item->appointmentID }}" id="appointmentID" value="{{ $item->appointmentID }}">
            <i class="fa fa-edit"></i> {{ trans('translate.edit') }} 
            </button>   

最佳答案

建议:

我建议像这样获取DOM元素:

$(document).ready(function() {
    $('.btn-teal').click(function() {
        // 1) Use .attr() method to get the given value based on what [attribute] (e.g data-id)
        // This will get the appointment value of the element click
        var appointmentID = $(this).attr('data-id'); 

        // 2) Then by the time you open the modal, You can assign it
        // on another DOM element
        $('#appointmentModal').find('input[name="appointmentID"]').val(appointmentID);

        // 3) After assigning it, Open that modal box
        $('#appointmentModal').modal('show');
    });
});

希望这对您的情况有所帮助

关于javascript - 将变量 id 传递到 Modal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44484354/

相关文章:

javascript - 用于 webpack 构建的分离的开发和生产配置文件

c# - 无法连接到集线器类

javascript - JQuery Mobile - 禁用 "enter"/script 以不允许使用 Enter

jquery-ui - Jquery 选项卡 : spinner option not working

javascript - 在 jQuery 内部修改在 jQuery 中创建的 html 元素

javascript - 如何从 html 页面中选择某些文本的位置获取 div 的 id

javascript - 查找 ActiveX COM 组件的版本

原生处理路由(改变状态)的Javascript框架

javascript - 如何垂直显示一组动态生成的按钮

javascript - 为什么折线图中 y 轴值从实际点向上移动?