简体   繁体   English

当按钮单击同一页面时,在引导模式中获取 id

[英]Get id in bootstrap modal when button click on same page

I have multiple buttons to invoke a bootstrap model in a page the buttons are generated through foreach loop based on db table rows example if db table has two rows the first row id is 1, and second row id is 2 that's mean tow buttons are generated and I have set specific id to each button and i want when user click on button the bootstrap popup modal will open and display the button id in modal which one the clicked.我有多个按钮来调用页面中的引导模型,这些按钮是通过基于 db 表行示例的 foreach 循环生成的,如果 db 表有两行,第一行 id 为 1,第二行 id 为 2,这意味着生成两个按钮并且我已经为每个按钮设置了特定的 id,我希望当用户点击按钮时,引导弹出模式将打开并以模式显示按钮 id,其中一个被点击。

I want to pas button holded id to modal within the same page我想在同一页面内将按钮持有的 id 传递到模态

You could refer to the following demo你可以参考下面的demo

View code查看代码

@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 code , <input type="text" name="id" id="id" /> in the modal-body to get the id of button模态代码,在模态体中<input type="text" name="id" id="id" />获取按钮的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 -->

In jQuery , get the specific id of each button through data-id="@item.Id" , and set the id to the <input type="text" name="id" id="id"/> in the modal-body.在jQuery中,通过data-id="@item.Id"获取每个按钮的具体id,并将id设置为data-id="@item.Id"中的<input type="text" name="id" id="id"/> -身体。

  @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>

How it works这个怎么运作

在此处输入图片说明

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

相关问题 单击按钮时将数据获取到模态,按钮具有相同的 ID - Get data to Modal on button click, with buttons that have the same ID 在Bootstrap模态中单击按钮以获取启动元素? - Click button in Bootstrap modal to get the launching element? 如何在Bootstrap模式中单击按钮获取数据 - How to get data on button click in Bootstrap modal 如何在单击时获取模式按钮上的行 ID - How to get row id on modal button on click 加载页面时显示带视频的模态,并带有单击按钮以打开相同模态的选项 - Show modal with video when loading page with option to click the button to open the same modal 在页面加载上启动Bootstrap模式,在上一页按钮上单击 - Launch Bootstrap Modal on Page Load, Contingent on Previous Page Button Click 单击取消按钮时返回到同一页面的Javascript代码 - Javascript code to get back to the same page when click the cancel button 引导模式是在按钮单击上,但在页面加载时隐藏在后台 - bootstrap modal is on button click but it is lying hidden in the background on page load 单击引导模式中的“保存”按钮后如何禁用刷新页面? - How to disable refresh page after click on Save button in bootstrap modal? 如何在图像点击的同一页面上的多个图像上使用bootstrap模式? - How to use bootstrap modal on multiple images on same page on image click?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM