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

时间:2010-05-21 11:45:33

标签: 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>

3 个答案:

答案 0 :(得分:18)

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

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

您的其余代码是正确的,但它目前只是关闭对话框并返回,您需要实际提交表单。此外,最好将此作为点击处理程序而不是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;
        });
    });

然后只需给<input> delete<input class="delete" type="submit" value="delete" /> ,就像这样:

{{1}}

答案 1 :(得分: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!
  });
});

答案 2 :(得分:1)

对于具有GET方法的表单,因为我还需要查询字符串中按钮的值,并避免在表单提交时丢失它的值

我在JavaScript脚本中使用了event.preventDefault(),并将按钮的名称和值添加为hidden <input> field

JS脚本

$( function() {
  //Add function to all document's html tag with class="delete"
  $(document).on("click", ".delete", function(event) {

    event.preventDefault(); //Stop submitting form and wait for a "yes" or "no"

    $( "#dialog-confirm" ).dialog({
      draggable: false,
      resizable: false,
      height: "auto",
      width: 400,
      modal: true,
      buttons: {
        "Delete items": function() {
          $( this ).dialog( "close" );

          //Prevent to lost button's value in query string at form submit
          //Need to add a hidden <input> field with the same button's name and value
          $('<input />').attr('type', 'hidden')
                        .attr('name', 'action')
                        .attr('value', 'delete')
                        .appendTo('#myform');

          $('#myform').submit(); //Ok proceed with form submit 

        },
        Cancel: function() {
          $( this ).dialog( "close" ); //Do nothing, close window.
        }
      }
    });
  });
});

表单模板

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

<form id="myform" action="#myurl" method="get">
  <label for="fname">First name:</label><br>
  <input type="text" id="fname" name="fname" value="John"><br>
  <label for="lname">Last name:</label><br>
  <input type="text" id="lname" name="lname" value="Doe"><br><br>
  <input class="delete" type="submit" name="action" value="delete">
</form>

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>jQuery UI Dialog - Modal confirmation</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  <script>
    $( function() {
      $(document).on("click", ".delete", function(event) {

        event.preventDefault(); //Stop submitting form and wait for a "yes" or "no"

        $( "#dialog-confirm" ).dialog({
          draggable: false,
          resizable: false,
          height: "auto",
          width: 400,
          modal: true,
          buttons: {
            "Delete items": function() {
              $( this ).dialog( "close" );
              
              //Prevent to lost button's value in query string at form submit
              //Need to add a hidden <input> field with the same button's name and value
              $('<input />').attr('type', 'hidden')
                            .attr('name', 'action')
                            .attr('value', 'delete')
                            .appendTo('#myform');

              $('#myform').submit(); //Ok proceed with form submit

            },
            Cancel: function() {
              $( this ).dialog( "close" ); //Do nothing, close window.
            }
          }
        });
      });
    });
  </script>
</head>
<body>
 
<div style="display:none" id="dialog-confirm" title="Empty the recycle bin?">
  <p><span class="ui-icon ui-icon-alert" style="float:left; margin:12px 12px 20px 0;"></span>These items will be permanently deleted and cannot be recovered. Are you sure?</p>
</div>

<form id="myform" action="#myurl" method="get">
  <label for="fname">First name:</label><br>
  <input type="text" id="fname" name="fname" value="John"><br>
  <label for="lname">Last name:</label><br>
  <input type="text" id="lname" name="lname" value="Doe"><br><br>
  <input class="delete" type="submit" name="action" value="delete">
</form>
 
</body>
</html>