jquery对话框:确认单击提交按钮

Omu*_*Omu 8 jquery

我正在尝试使用jquery进行确认对话,但表单根本没有提交,这就是我得到的:

    <script type="text/javascript">
    var currentForm;
    $(function() {
        $("#dialog-confirm").dialog({
            resizable: false,
            height: 140,
            modal: true,
            autoOpen: false,
            buttons: {
                'Delete all items': function() {
                    currentForm.submit();
                    $(this).dialog('close');
                },
                Cancel: function() {
                    $(this).dialog('close');
                }
            }
        });
    });

    function ask() {
        currentForm = $(this).closest('form');
        $("#dialog-confirm").dialog('open');
    }
</script>
<form ... >
    <input type="submit" value="delete" onclick="ask();return false;" />
</form>
Run Code Online (Sandbox Code Playgroud)

Nic*_*ver 18

您需要让对话框按钮提交<form>,如下所示:

               'Delete all items': function() {
                  $(this).dialog('close');
                  $("#myForm").submit();
                }
Run Code Online (Sandbox Code Playgroud)

其余代码是正确的,但它目前只是关闭对话框并返回,您需要实际提交表单.此外,最好将此作为点击处理程序,而不是onclick像这样(为评论更新):

    var currentForm;
    $(function() {
        $("#dialog-confirm").dialog({
            resizable: false,
            height: 140,
            modal: true,
            autoOpen: false,
            buttons: {
                'Delete all items': function() {
                    $(this).dialog('close');
                    currentForm.submit();
                },
                'Cancel': function() {
                    $(this).dialog('close');
                }
            }
        });
        $(".delete").click(function() {
          currentForm = $(this).closest('form');
          $("#dialog-confirm").dialog('open');
          return false;
        });
    });
Run Code Online (Sandbox Code Playgroud)

那么就给你<input>一个类delete,如下:

<input class="delete" type="submit" value="delete" />
Run Code Online (Sandbox Code Playgroud)

  • @Omu - Doh,我的错,甚至没有看到编辑,感谢为未来的googlers改进这个:) (2认同)

Seb*_*Seb 5

如果您正在使用jQuery,那么请正确执行并避免使用内联Javascript:

$(function (){
  $("form").submit(function (){
    // return false if you don't want this to be submitted
    // maybe do a
    // return ask();
    // but with the code provided it doesn't seem
    // to work like that - ask() is not returning anything at all!
  });
});
Run Code Online (Sandbox Code Playgroud)