【问题标题】:jQuery, Using .remove() , but I would like a confirmation box to appearjQuery,使用 .remove() ,但我想出现一个确认框
【发布时间】:2011-03-20 19:55:12
【问题描述】:

我正在使用 jQuery 的 .remove() 删除内容的 div,但我希望能够在删除之前显示确认或对话框。我只是不确定如何编写这样的对话。

$(".remove").click(function () {
  $('div').remove('#item1');
});

单击带有类删除的链接后,我希望弹出一个框,说您确定要删除它吗?然后单击“是”删除,单击“否”保留。提前致谢

【问题讨论】:

    标签: jquery confirmation


    【解决方案1】:

    试试这个:

    $(".remove").click(function () {
      if(confirm("are you sure you want to remove the div")){
        $('div').remove('#item1');
      }
    });
    

    希望对你有帮助

    【讨论】:

      【解决方案2】:
      $(".remove").click(function () {
          if (confirm('Are you sure?')) {
              $('div').remove('#item1');
          }
      });
      

      【讨论】:

        【解决方案3】:
        $(".remove").click(function () {
          if(confirm("Are you sure you want to remove this?")) {
            $('div').remove('#item1');
          }
        });
        

        【讨论】:

          【解决方案4】:

          试试window.confirmhttps://developer.mozilla.org/en/window.confirm

          $(".remove").click(function () {
            if(window.confirm('Are you sure?')) {
              $('div').remove('#item1');
            }
          });
          

          【讨论】:

            【解决方案5】:

            我认为,如果您正在寻找更好的确认对话框,那么浏览器上的默认设置会为您提供。

            看看jQueryUI confirm dialog 您可以根据需要设置样式

            这就是你实现它的方式:

              <div id="dialog" title="Confirmation Required">
              Are you sure about this?
              </div>
            
            <script type="text/javascript">
            $(document).ready(function() {
            $("#dialog").dialog({
              autoOpen: false,
              modal: true
            });
             });
            
            
            
            $("#dialog").dialog({
              buttons : {
                "Confirm" : function() {
                // do something remove()
                },
                "Cancel" : function() {
                  $(this).dialog("close");
                }
              }
            });
            
               $("#dialog").dialog("open");
             });
            

            【讨论】:

            • 太棒了,我一定会调查的。它比默认的更吸引人,而且似乎对 IE 友好
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2013-02-28
            • 1970-01-01
            • 1970-01-01
            • 2017-09-12
            • 1970-01-01
            • 2012-05-26
            • 2014-08-28
            相关资源
            最近更新 更多