Jquery确认不起作用

时间:2016-03-03 09:13:55

标签: jquery jquery-ui confirm confirmation

我试过了:https://stackoverflow.com/a/12617274/4164311。字符串“你确定这个”只是出现在我的浏览器中,当我点击按钮时没有任何反应。

<html lang="en">
<head>
  <meta charset="utf-8">
  <title>jQuery UI Dialog - Modal confirmation</title>

  <link rel="stylesheet" href="jquery-ui.min.css">
  <script src="external/jquery/jquery.js"></script>
  <script src="jquery-ui.min.js"></script>

  <script>
   $("#dialog").dialog({
   autoOpen: false,
   modal: true,
   buttons : {
        "Confirm" : function() {
            alert("You have confirmed!");            
        },
        "Cancel" : function() {
          $(this).dialog("close");
        }
      }
    });

    $("#callConfirm").on("click", function(e) {
      e.preventDefault();
      $("#dialog").dialog("open");
    });
  </script>
</head>
<body>
<button id="callConfirm">Confirm!</button>

<div id="dialog" title="Confirmation Required">
  Are you sure about this?
</div>
</body>
</html>

截图:

enter image description here

2 个答案:

答案 0 :(得分:4)

尝试将js代码放在文档就绪函数中,它应该可以正常工作。

$(function() {
  $("#dialog").dialog({
    autoOpen: false,
    modal: true,
    buttons: {
      "Confirm": function() {
        alert("You have confirmed!");
      },
      "Cancel": function() {
        $(this).dialog("close");
      }
    }
  });

  $("#callConfirm").on("click", function(e) {
    e.preventDefault();
    $("#dialog").dialog("open");
  });
});

希望这有帮助。

编辑:另外,添加/更改jquery和css包含到:

 <link rel="stylesheet" href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
  <script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
  <script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

答案 1 :(得分:0)

注意:你需要放在cdn链​​接下面

<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/jquery-
       confirm/3.3.2/jquery-confirm.min.css">   
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery- 
       confirm/3.3.2/jquery- confirm.min.js"></script>


 $("#btnSubmit").click(function(){

     $.confirm({

     title: 'Confirmation !!',

      content: 'Are you sure, delete this item?',

       buttons: {

        confirm: function () {
         // write your logic    
                 },      
         cancel: function () { 

        } }

        });


        });