【问题标题】:Get id in bootstrap modal when button click on same page当按钮单击同一页面时,在引导模式中获取 id
【发布时间】:2018-12-31 23:23:04
【问题描述】:

我有多个按钮来调用页面中的引导模型按钮是通过基于 db 表行示例的 foreach 循环生成的,如果 db 表有两行,第一行 id 为 1,第二行 id 为 2,这意味着拖生成了按钮,并且我为每个按钮设置了特定的 ID,我希望当用户单击按钮时,引导弹出模式将打开并在模式中显示按钮 ID,其中一个被单击。

我想将按钮持有的 id 传递给同一页面内的模态

【问题讨论】:

    标签: javascript jquery ajax asp.net-core asp.net-core-signalr


    【解决方案1】:

    你可以参考下面的demo

    查看代码

    @model IEnumerable<MVC2_1Test.Models.City>
    
    @{
    ViewData["Title"] = "Index";
    }
    
    <h2>Index</h2>
    
    <p>
    <a asp-action="Create">Create New</a>
    </p>
    <table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.CityName)
            </th>
            <th>
    
            </th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.CityName)
                </td>
                <td>
                    <button data-id="@item.Id" data-toggle="modal" data-target="#myModal" class="ModalClick"> Edit </button>
                </td>
            </tr>
        }
    </tbody>
    

    模态代码,在modal-body中&lt;input type="text" name="id" id="id" /&gt;获取按钮的id

    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">Details</h4>
            </div>
            <div class="modal-body">
                <div class="form-group">
                    <label class="control-label">Id:</label>
                    <input type="text" name="id" id="id" />
                </div>
    
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
    

    在jQuery中,通过data-id="@item.Id"获取每个按钮的具体id,并将id设置为modal-body中的&lt;input type="text" name="id" id="id"/&gt;

      @section Scripts
    {
    <script type="text/javascript">
        $(".btnModal").click(function () {
            var passedID = $(this).data('id');//get the id of the selected button
            $('input:text').val(passedID);//set the id to the input on the modal
        });
    </script>
    

    它是如何工作的

    【讨论】:

    • 在模态中获取数据,无需重新加载页面或进入控制器获取数据,通过控制器其简单的即时通讯也可以做到。请给出与我的问题相关的答案。谢谢你
    • @Jagdish Kumar,我已经更新了我的回复并展示了它是如何工作的。 modal 获取 id 时页面没有重新加载。您是否尝试过测试代码?如果您有任何其他问题,请分享可以重现问题的相关代码。
    • 它在没有 js 的情况下完成,现在我想在不重新加载页面的情况下编辑和删除该记录。请帮忙
    猜你喜欢
    • 2018-06-15
    • 2015-10-02
    • 2016-01-09
    • 2015-04-07
    • 1970-01-01
    • 1970-01-01
    • 2021-09-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多