【问题标题】:jquery dialog post if true如果为真,则 jquery 对话框发布
【发布时间】:2020-03-08 18:21:00
【问题描述】:

我有以下代码示例,并且我仅在用户单击“是”按钮时才尝试发出发布请求。如果用户单击“否”按钮,则不会发生任何事情。感谢您的帮助和提示。

<html>
<head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <link href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css"
        rel="stylesheet" />
    <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
</head>
<body>
    <form action="" method="post">
        <button id="save" onclick="return ConfirmDialog('this is a test')">Save</button>
    </form>

<script>
    function ConfirmDialog(message) {
        $('<div></div>').appendTo('body')
            .html('<div><h6>' + message + '?</h6></div>')
            .dialog({
                modal: true,
                title: 'Delete message to use',
                zIndex: 10000,
                autoOpen: true,
                width: 'auto',
                resizable: false,
                width: 400,
                buttons: {
                    Yes: function () {
                        // $(obj).removeAttr('onclick');                                
                        // $(obj).parents('.Parent').remove();

                        //$('body').append('<h1>Confirm Dialog Result: <i>Yes</i></h1>');
                        $(this).dialog("close");
                        return true;
                    },
                    No: function () {
                        //$('body').append('<h1>Confirm Dialog Result: <i>No</i></h1>');
                        $(this).dialog("close");
                        return false;
                    }
                },
                close: function (event, ui) {
                    $(this).remove();
                }
            });
    };
</script>     
</body>
</html>

【问题讨论】:

    标签: javascript jquery dialog


    【解决方案1】:

    问题是您的按钮默认为type="submit",您可以将其更改为type="button"并正常执行您的功能:

    function ConfirmDialog(message) {
      $('<div></div>').appendTo('body')
        .html('<div><h6>' + message + '?</h6></div>')
        .dialog({
          modal: true,
          title: 'Delete message to use',
          zIndex: 10000,
          autoOpen: true,
          width: 'auto',
          resizable: false,
          width: 400,
          buttons: {
            Yes: function() {
              $('body').append('<h1>Confirm Dialog Result: <i>Yes</i></h1>');
              $(this).dialog("close");
              return true;
            },
            No: function() {
              $('body').append('<h1>Confirm Dialog Result: <i>No</i></h1>');
              $(this).dialog("close");
              return false;
            }
          },
          close: function(event, ui) {
            $(this).remove();
          }
        });
    };
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <link href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css" rel="stylesheet" />
    <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
    
    <form action="" method="post">
      <button type="button" id="save" onclick="return ConfirmDialog('this is a test')">Save</button>
    </form>

    【讨论】:

    • 但是当我点击是时,表单没有发布:-(
    • 点击Yes需要使用$('form').submit()
    猜你喜欢
    • 2018-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-24
    • 2015-11-17
    • 2021-01-07
    • 1970-01-01
    相关资源
    最近更新 更多