首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HTML动态弹出框

HTML动态弹出框
EN

Stack Overflow用户
提问于 2017-05-16 11:19:02
回答 1查看 101关注 0票数 0

在我的jsp中,我有一个for each循环(spring mvc)。

代码语言:javascript
复制
<c:forEach items="${ce}" var="contractEntitlement">
    <c:if test="${contractHeader.id ==  contractEntitlement.chId}" >
        <tr>
            TD's to show each fields ....
            <td>
                <div class="center">
                    <a href="/view/viewcontract/update/ce/${contractEntitlement.id}" id="id-btn-dialog2" class="btn btn-info btn-sm">Confirm Dialog</a>
                </div>
                <div id="dialog-confirm" class="hide">
                    <div class="alert alert-info bigger-110">
                        These items will be permanently deleted and cannot be recovered.
                    </div>

                    <div class="space-6"></div>

                    <p class="bigger-110 bolder center grey">
                        Are you sure?
                    </p>
                </div><!-- #dialog-confirm -->
            </td>
        </tr>
    </c:if>
</c:forEach>

每条记录都有一个用于删除的按钮。我的jquery是:

代码语言:javascript
复制
$( "#id-btn-dialog2" ).on('click', function(e) {
    e.preventDefault();
    var href = ($(this).attr('href'));
    alert(href);

    $( "#dialog-confirm" ).removeClass('hide').dialog({
        resizable: false,
        width: '320',
        modal: true,
        buttons: [
            {
                "class" : "btn btn-danger btn-minier",
                click: function() {
                    $( this ).dialog( "close" );
                    window.location = href;
                }
            }
            ,
            {
                "class" : "btn btn-minier",
                click: function() {
                    $( this ).dialog( "close" );
                }
            }
        ]
    });
}); 

这只适用于第一条记录,但当我单击其余记录时,它只会重定向到没有传递jquery的页面。我尝试基于这个堆栈溢出问题进行更改:

Passing data to a jQuery UI Dialog

代码语言:javascript
复制
$( 'a[href*="/view/viewcontract/update/ce/${contractEntitlement.id}"]' ).on('click', function(e) {

但是这样一来,即使是第一条记录也不会再传递到jquery中。你知道怎么让我的jquery更有活力吗?

EN

回答 1

Stack Overflow用户

发布于 2017-05-16 14:49:39

如前所述,需要唯一的id才能正常工作。在jquery上,当DOM准备就绪时:

代码语言:javascript
复制
var i=0;
$('.testing').each(function(){     // testing is the class name
    i++;
    var newID='id-btn-dialog2'+i;  // increment the id +1 (unique)
    $(this).attr('id',newID);      // Assign the new id
    $(this).val(i);
});

现在我们有了唯一的id,需要得到点击的按钮的id并弹出消息框。在jquery中:

代码语言:javascript
复制
$('a.testing').on('click', function(e) { 
    var id = $(this).attr('id');
    var href = $(this).attr('href');
    alert(href);
    e.preventDefault();

    $( "#dialog-confirm" ).removeClass('hide').dialog({
        resizable: false,
        width: '320',
        modal: true,
        buttons: [
            {
                "class" : "btn btn-danger btn-minier",
                click: function() {
                    $( this ).dialog( "close" );
                    window.location = href;
                }
            }
            ,
            {
                "class" : "btn btn-minier",
                click: function() {
                    $( this ).dialog( "close" );
                }
            }
        ]
    });

}); 

这个方法很有效,并且能够解决我的问题。上面的jsp是相同的,除了:

代码语言:javascript
复制
<a href="/view/viewcontract/update/ce/${contractEntitlement.id}" id="id-btn-dialog2" class="btn btn-info btn-sm testing">Confirm Dialog</a>

添加“测试”类。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43991931

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档