【问题标题】:Javascript Confirm change into jquery modal dialogJavascript 确认更改为 jquery 模式对话框
【发布时间】:2014-03-20 06:06:24
【问题描述】:

我在这里有一个通过 ajax 删除的记录。如果我想删除记录,我会添加确认消息。我需要的是将确认消息更改为模态对话框http://jqueryui.com/dialog/#modal-confirmation

我想更改这个 javascript 代码

if(confirm("All PR and PO in this record will be deleted. Are you want to delete?"))

进入这个 jquery 模态对话框。有什么帮助吗?

<script>
$(function () {
  $("#dialog-confirm").dialog({
    resizable: false,
    height: 140,
    modal: true,
    buttons: {
      "Delete all items": function () {
        $(this).dialog("close");
      },
      Cancel: function () {
        $(this).dialog("close");
      }
    }
  });
});
</script>

Ajax 删除

<script>
$(function () {
  $(".delbutton").click(function () {
    //Save the link in a variable called element
    var element = $(this);
    //Find the id of the link that was clicked
    var del_id = element.attr("name");
    //Built a url to send
    var info = 'name=' + del_id;
    if (confirm("All PR and PO in this record will be deleted. Are you want to delete?")) {
      $.ajax({
        type: "GET",
        url: "delete.php",
        data: info,
        success: function () {}
      });
      $(this).parents(".record").animate({
        backgroundColor: "#fbc7c7"
      }, "fast")
        .animate({
          opacity: "hide"
        }, "slow");
    }
    return false;
  });
});
</script>

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    您将在您的 html 代码中覆盖 javascript 的确认方法,如下所示

     <script>
     function confirm(message) {
     var myTitle = 
            "<div style='float: left'>Error</div><div style='float: right; margin-right: 15px;'>Close</div>";
            $('<div id="dlgTest1" style="display: none;min-height:auto;"><p style="text-align:justify;font-family:verdana;font-weight: bold;">'+message+'</p></div>').dialog({
    resizable: false,
            modal: true,
            width: 300,
            height: 'auto',
            bgiframe: false,
            //position: ['top', 5],
            draggable: true,
            closeOnEscape: true,
            minHeight:20,
            buttons: [{
                text: "Cancel",
                "style": 'background-color:#CCCCCC !important;color:rgb(119, 119, 119);font-family:verdana',
                  click:function(){
                    $(this).dialog('close');
                    return false;
                }
    
            },{
            text: "Ok",
            "style": 'background-color:#007AC0 !important;color:white;font-family:verdana',
              click:function(){
                $(this).dialog('close');
            }
            }
            ],
            close:function(){ $(this).dialog('destroy').remove(); }
        }).siblings('.ui-dialog-titlebar').append(myTitle); // title goes here;
        //$("#dlgTest1").dialog("open").dialog("moveToTop");
    };
    

    然后使用它,但是注意不要在上面使用html提交按钮,使用html普通按钮

    【讨论】:

    • 我只复制整个代码和我的 ajax 而不做任何更改?
    【解决方案2】:
     //try this code   
    
     <script>
        var isConfirmed=false;
        $(function () {
          $("#dialog-confirm").dialog({
            resizable: false,
            height: 140,
            modal: true,
            buttons: {
              "Delete all items": function () {
                 isConfirmed=true;
                $(this).dialog("close");
              },
              Cancel: function () {
                isConfirmed=false;
                $(this).dialog("close");
              }
            }
          });
    
    
        $(".delbutton").click(function () {
            //Save the link in a variable called element
            var element = $(this);
            //Find the id of the link that was clicked
            var del_id = element.attr("name");
            //Built a url to send
            var info = 'name=' + del_id;
            $("#dialog-confirm").html("All PR and PO in this record will be deleted. Are you want to delete?");
            if (isConfirmed) {
              $.ajax({
                type: "GET",
                url: "delete.php",
                data: info,
                success: function () {}
              });
              $(this).parents(".record").animate({
                backgroundColor: "#fbc7c7"
              }, "fast")
                .animate({
                  opacity: "hide"
                }, "slow");
            }
            return false;
          });
        });
        </script>
    

    【讨论】:

    • 检查使用过的确认对话框如何打开,有没有 $("#dialog-confirm").dialog('open')
    • 是的,我在 $("#dialog-confirm").html() 添加行 $("#dialog-confirm").dialog(); 后得到它;
    • 这样吗? $("#dialog-confirm").html("这条记录中的所有PR和PO都将被删除,您要删除吗?"); $("#dialog-confirm").dialog();
    • 你试过$("#dialog-confirm").dialog('open')这个
    • 您必须在单个脚本标签中编写此代码,用此代码替换您的脚本代码
    【解决方案3】:
    //replace your script with this code and try
    
     <script>
        var isConfirmed=false;
        $(function () {
          $("#dialog-confirm").dialog({
            resizable: false,
            height: 140,
            modal: true,
            buttons: {
              "Delete all items": function () {
                 isConfirmed=true;
                $(this).dialog("close");
              },
              Cancel: function () {
                isConfirmed=false;
                $(this).dialog("close");
              }
            }
          });
    
    
        $(".delbutton").click(function () {
            //Save the link in a variable called element
            var element = $(this);
            //Find the id of the link that was clicked
            var del_id = element.attr("name");
            //Built a url to send
            var info = 'name=' + del_id;
            $("#dialog-confirm").html("All PR and PO in this record will be deleted. Are you want to delete?");
    $("#dialog-confirm").dialog();
            if (isConfirmed) {
              $.ajax({
                type: "GET",
                url: "delete.php",
                data: info,
                success: function () {}
              });
              $(this).parents(".record").animate({
                backgroundColor: "#fbc7c7"
              }, "fast")
                .animate({
                  opacity: "hide"
                }, "slow");
            }
            return false;
          });
        });
        </script>
    

    【讨论】:

      猜你喜欢
      • 2012-03-23
      • 2014-10-17
      • 1970-01-01
      • 1970-01-01
      • 2011-05-24
      • 2013-12-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多