如何在JQuery UI对话框中将事件处理程序分配给自定义按钮

时间:2015-07-31 10:03:32

标签: jquery user-interface javascript-events

在我的html中,我有这样的代码:Jquery UI对话框的链接和div:

 <a data-comment="delete" data-comment-url="<?=Yii::app()->createUrl('comments/delete', ['id' => $comment->id])?>" href="javascript:" ">              
  </a>
 <!-- div with dialog's markup -->
 <div id="commentDelete" class="modal smallDialog" title="УДАЛЕНИЕ">
                            Delete this row &
 <a  class="button142 wa" id="confirmDeleteComment"  style="">Yes</a>
 <a href="javascript:;" onClick="$('#commentDelete').dialog('close');" class="button142 wa">No</a>
                        </div>

在我的外部js文件中,我有事件处理程序来捕获链接上的点击:

$(document).on('click', '[data-comment="delete"]', function (evt) {
    evt.preventDefault();

    var data = $.data(document, 'comments'),
        $this = $(this);

    var options = {
        title: 'Delete row?',
        autoOpen: false,
        resizable: false,
        height: 200,
        width: 380,
        show: { effect: 'drop', direction: "up" },
        modal: true,
        draggable: true,
        open: function (event, ui) {

        },
        close: function (event, ui) {
            $("#commentDelete").dialog().dialog('close');
        },
        create: function(event, ui){
            $('#confirmDeleteComment').click(function(){
               console.log("!!!");
            });
        }
    }
    $("#commentDelete").dialog(options).dialog('open');

当我点击链接时,此事件触发很好,没问题。但我希望在函数open()内部我可以将我的动作分配给div的第一个按钮,它在UI对话框中使用。跟踪代码,我看到open()被激活,点击分配传递得很好,没有错误,但是当我尝试点击按钮时 - 我没有按照我的考虑采取任何行动 - 没有登录控制台。

我做错了什么?

1 个答案:

答案 0 :(得分:1)

您可以将其委托给模态div级别:

$('#commentDelete').on('click', '#confirmDeleteComment', function () {
    console.log("!!!");
});
相关问题