Jquery对话框确认,确认表单发布

时间:2010-05-29 16:16:29

标签: jquery dialog confirmation

我在提交表单之前尝试显示jquery的对话框确认。但是,只有在提交表单时才能弹出它,这是代码:

$(function remove() {                           
    $("#dialog-confirm").dialog({
        resizable: false,
        height:200,
        modal: true,
        buttons: {
            'Delete campaign': function() {
                return true ;
                $(this).dialog('close');
            },
            Cancel: function() {
                return false;
                $(this).dialog('close');
            }
        }
    });
});

对话确认内容

  <div id="dialog-confirm" title="Delete ?" style="display:none;">
    <p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>This will be permanently deleted and cannot be recovered. Are you sure?</p>
</div>

表单提交内容

<form style="display: inline;" action="remove.php" method="post"" onsubmit="return remove()">

1 个答案:

答案 0 :(得分:1)

函数remove不应放在$(...);中,因为$(function(){})是在文档加载时自动执行的东西,只需将函数移动到明确定义的位置即可根。我还建议使用内联回调;在表单上设置id并使用以下代码:

function remove() {
  ...
}
$(function(){
  $('#formid').submit(remove);
  // normal initializing code here, which is executed when document is ready
})

或者您也可以直接定义回调:

$(function(){
  $('#formid').submit(function(){
    // same code as in remove function above
  });
  // normal initializing code here, which is executed when document is ready
})