点击jQuery按钮确认对话框

时间:2017-09-05 19:35:19

标签: javascript jquery html jquery-ui razor

这可能是重复的,但我无法找到答案。我有一个按钮,它正在进行ajax调用并在点击时执行功能。但是我需要通过弹出确认来限制它#34;您确定要删除此帐户吗?"与"是","否"选项。我使用单个按钮删除/添加帐户,因此确认对话框上的文字应相应更改。

<button type="submit" id="StatusChange" name="StatusChange" class="btn btn-primary pull-right">
  "Click to Delete Account" 
</button>

$("#StatusChange").click(function () {
  var state = 'ACTIVE';
  var id = '12345';
  $.ajax({
    type: "POST",
    url: '@Url.Action("ChangeAccountStatus", "Account")',
    data: { currentStatus: state, accountId:id },
    success: function(data){
      location.reload();
    }
  });
});

谢谢!

4 个答案:

答案 0 :(得分:2)

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

 var x=confirm( "Are you sure you want to delete?!");
           if(x){
            var state = 'ACTIVE';
            var id = '12345';
            $.ajax({
                type: "POST",
                url: '@Url.Action("ChangeAccountStatus", "Account")',
                data: { currentStatus: state, accountId:id },
                success: function(data) 
                {
                   location.reload();
                }
            });
         }
   });

样品:

$("#StatusChange").click(function () {
       var num=$("#Num").val();
       var x=confirm( num==1?"Are you sure you want to delete?!":"Are you sure you want to Update?!");
       if(x){
        if(num==1) 
         $("div").remove();
        else
          $("div").text("test Update");
       }
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="Num" type="number" value="1"/>
<input type="button" id="StatusChange" value="delete"/>
<br>
<div>Test</div>

答案 1 :(得分:1)

使用标准的javascript确认对话框,如下所示:

$("#StatusChange").click(function () {
var result = confirm("Want to delete?");
if (result) {
    var state = 'ACTIVE';
    var id = '12345';    
    $.ajax({
          type: "POST",
          url: '@Url.Action("ChangeAccountStatus", "Account")',
          data: { currentStatus: state, accountId:id },
          success: function(data){location.reload();}
          });
}                
});

您可以阅读更多here

答案 2 :(得分:1)

$("#StatusChange").click(function() {
  var state = 'ACTIVE';
  var id = '12345';

  if (confirm("Are you sure to delete this account?")) {
    $.ajax({
      type: "POST",
      url: '@Url.Action("ChangeAccountStatus", "Account")',
      data: {
        currentStatus: state,
        accountId: id
      },
      success: function(data) {
        location.reload();
      }
    });
  }  // can optionally add an else statement here if user cancelled
});

答案 3 :(得分:1)

像这样:

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

if(confirm("Are you sure you wish to continue?")){
            var state = 'ACTIVE';
            var id = '12345';
            $.ajax({
                type: "POST",
                url: '@Url.Action("ChangeAccountStatus", "Account")',
                data: { currentStatus: state, accountId:id },
                success: function(data) 
                {
                   location.reload();
                }
            });
}
});
相关问题