重新绑定jquery对话框/模态

时间:2012-09-05 03:56:22

标签: jquery dialog modal-dialog

感谢早期的帮助(http://stackoverflow.com/questions/12224717/cant-execute-jquery-post-or-ajax-after-initial-run),重新绑定点击事件现在运行正常。但是,我似乎无法重新绑定对话框定义。从这里的其他答案中读到,似乎答案在于使用“委托”,但我似乎无法弄明白。这是使用对话框的更新代码:

$('[id^="del"]').live('click', function(){
    var valname = $(this).attr('id').split('del_');
    $.post("delthis.php", {id: ""+valname[1]+""}, function(data) {
            $("#dynamic_section").html(data);
    });
    return false;
});

$('[id^="thismodal"]').dialog({
    autoOpen: false,
    show: 'blind',
    hide: 'explode',
    width: '760',
    resizable: false,
    modal: true
});

// Dialog Link
$('[id^="mlink"]').live('click', function(e) {
    e.preventDefault();
    var valname = $(this).attr('id');
    var sname = valname.split("mlink_");
    var modal = "#thismodal" + sname[1];
    $(modal).dialog('open');
    //return false;
});

// html
// loop
<div>
<p>
   <a href='#' id='del_<?php echo $row['id']; ?>'>Delete <?php echo $row['id']; ?></a><br />
  <a href='#' id='mlink_<?php echo $row['id']; ?>'>Show Modal <?php echo $row['id']; ?></a>
  <div id='thismodal<?php echo $row['id']; ?>'>
    This is the modal showing element # <?php echo $row['id']; ?>
  </div>
 </p>
</div>
// end loop

谢谢。

1 个答案:

答案 0 :(得分:0)

这个SO问题有很多帮助:How do I rebind the dialog after it is being rewritten by AJAX?

所以对于我上面的问题,我刚刚创建了:

var initModal = function () { 
    $('[id^="thismodal"]').dialog({
        autoOpen: false,
        show: 'blind',
        hide: 'explode',
        width: '760',
        resizable: false,
        modal: true
    });
};

每次重新填充html时都会调用它