在jQuery中为多个表行简单弹出?

时间:2015-09-17 10:47:37

标签: javascript jquery popup

我想在我的表格中放一个简单的弹出窗口,以显示有关每一行的其他详细信息。我只是想问一下如何在每一行显示弹出窗口的想法。目前弹出窗口仅显示第一个。

这是我的代码:

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

脚本:

 <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;
    });
})

查看Pop:

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

2 个答案:

答案 0 :(得分:1)

您应该使用公共类

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

然后您可以使用Class Selector (“.class”)

  

选择具有给定类的所有元素。

脚本

$('.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;
});

答案 1 :(得分:0)

$('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

这样您就可以获得单击按钮的行的所有数据