admin管理员组

文章数量:1431531

This might be a duplicate, but I couldn't find an answer. I have a button which is making an ajax call and doing the functionality on click. But I need to restrict that by giving a confirmation pop up "Are you sure to delete this account?" with "Yes", "No" options. Im using a single button to delete/add accounts, so the text on confirmation dialogue should change accordingly.

<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();
    }
  });
});

Thanks!

This might be a duplicate, but I couldn't find an answer. I have a button which is making an ajax call and doing the functionality on click. But I need to restrict that by giving a confirmation pop up "Are you sure to delete this account?" with "Yes", "No" options. Im using a single button to delete/add accounts, so the text on confirmation dialogue should change accordingly.

<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();
    }
  });
});

Thanks!

Share Improve this question edited Sep 5, 2017 at 20:33 Pradvaar cruz asked Sep 5, 2017 at 19:35 Pradvaar cruzPradvaar cruz 2911 gold badge9 silver badges24 bronze badges 1
  • Possible duplicate of Intercepting a jQuery.ajax() call with confirm() – showdev Commented Sep 5, 2017 at 20:05
Add a ment  | 

4 Answers 4

Reset to default 3
$("#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();
                }
            });
         }
   });

Sample:

$("#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./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>

Use standard javascript confirm dialog, like this:

$("#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();}
          });
}                
});

You can read more here.

Like so:

$("#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();
                }
            });
}
});
$("#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
});

本文标签: javascriptConfirmation dialogue on button click for jQueryStack Overflow