Simple pop up in jQuery for multiple table rows?

落花浮王杯 提交于 2020-01-07 01:48:30

问题


I would like to put a simple pop up in my table to display additional details regarding each row. I just want to ask for ideas on how to show the pop up in every row. Currently the pop up is only shown for the first one.

Here's my code:

@foreach (var name in Model){
<table>
        <tr>
                 <td>Name</td>
                <td>  <button id="@name.id">Show</button></td>
        </tr>
</table>
}

Script:

 <script type="text/javascript">
$(function() {
    $('#').click(function() { //what should I put in the #? considering that it would have different id's?
        $("#popupdiv").dialog({
            title: "jQuery Popup from Server Side",
            width: 430,
            height: 250,
            modal: true,
            buttons: {
                Close: function() {
                    $(this).dialog('close');
                }
            }
        });
        return false;
    });
})

View Pop:

<div id="popupdiv" title="Basic modal dialog" style="display: none">
                <b> Welcome </b>
</div>

回答1:


You should use a common class

<td> <button id="@name.id" type="button" class='popup-launcher'>Show</button></td>

Then you can use Class Selector (“.class”)

Selects all elements with the given class.

Script

$('.popup-launcher').click(function() {
    //You can access elements id using this object
    var id = this.id;

   //Rest of your code
    $("#popupdiv").dialog({
        title: "jQuery Popup from Server Side",
        width: 430,
        height: 250,
        modal: true,
        buttons: {
            Close: function() {
                $(this).dialog('close');
            }
        }
    });
    return false;
});



回答2:


$('button').click(function () {
    var row = $(this).closest('tr').index();
    alert('In row ' + row)
    //$('table').find('tr:eq('+row+')').find('td:nth-child(1)');
    //pop code here 
})

FIDDLE

This way you can get all the data of the row where you click the button



来源:https://stackoverflow.com/questions/32628473/simple-pop-up-in-jquery-for-multiple-table-rows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!