2011-08-18 60 views
2

我有一个表,当任何行被点击时,启动一个jQueryUI模式对话框,允许用户编辑该记录。我用下面的脚本,似乎工作,成功加载使用AJAX相关记录的详细信息:jQueryUI模式对话框中的脚本只能工作一次

$("#datatbl tr").bind('click', function() {   
     var url = 'item_edit.asp?id='+$(this).attr("data-myid"); 
     var dialog = $('<div style="display:hidden" title="Record details:"></div>').appendTo('body'); 
     // load remote content 
     dialog.load(
      url, 
      {}, 
      function (responseText, textStatus, XMLHttpRequest) { 
       dialog.dialog({   
        height: 440, 
        width: 550, 
        autoOpen: false, 
        modal: true, 
        buttons: { 
         "Update this record": function() { 
         $('#editform').submit(); 
          }, 
         Cancel: function() { 
         $(this).dialog("close"); 
         } 
        } 
     }); 
     dialog.dialog('open'); 
      } 
     ); 
     //prevent the browser to follow the link 
     return false; 
}); 

它工作正常,我第一次点击的记录,但如果我点击取消,并尝试编辑再创纪录,对话框确实出现(具有正确的记录细节),但是,对话框内没有任何脚本工作 - 例如:有一个jqueryUI datepicker输入和一些验证。

没有javascript错误,并且从我对FireBug的有限理解中,我无法发现任何错误,所以我将不胜感激一些建议如何继续,谢谢!

编辑:啊!有时候,需要在这里输入一些信息来发现显而易见的东西。我刚刚意识到为对话框创建的DIV在框关闭时不会被破坏。我已经添加了一条线来做到这一点,它现在可以工作。感谢收听。 :)

以供将来参考,我添加了一个ID在“VAR对话”创建的DIV和取消功能删除它:

Cancel: function() { 
         $(this).dialog("close"); 
         $('#dialogbox').remove(); 
         } 

我还是很感激,如果有人提出一个更好的处理这种行为的方式。

+0

你应该添加您的编辑到答案,并接受了答案。 – Chamilyan

+0

我试过了,但需要等8个小时。我会去做,但同时我会编辑原文,谢谢。 – Stevemid

回答

1

我解决了这个问题:当对话框关闭时,为对话框创建的DIV不会被破坏。

我添加了一个ID在“VAR对话框”创建DIV并删除在取消功能的DIV:

Cancel: function() { 
         $(this).dialog("close"); 
         $('#dialogbox').remove(); 
         } 
0

您只能一次创建对话框,而不是每次载入其内容,只需将autoOpen设置为false即可。

<div id="dialog"> 
    <div id="content" style="display:hidden" title="Record details:"></div> 
</div> 

$('#dialog').dialog({ 
    height: 440, 
    width: 550, 
    autoOpen: false, 
    modal: true, 
    buttons: { 
     "Update this record": function() { 
      $('#editform').submit(); 
     }, 
     Cancel: function() { 
      $(this).dialog("close"); 
     } 
    } 
}); 
$("#datatbl tr").bind('click', function() {   
    var url = 'item_edit.asp?id='+$(this).attr("data-myid"); 
    // load remote content 
    $('#content').load(url); 
    $('#dialog').dialog('open'); 
    return false; 
}}; 
+0

将autoOpen 设置为false为。打开对话框不是问题,这是事实上,如果打开,关闭然后再次打开,盒子不起作用。 (往上看)。感谢您的回复 – Stevemid