【问题标题】:Confirmation dialogue on button click for jQueryjQuery的按钮点击确认对话框
【发布时间】:2017-09-05 19:35:18
【问题描述】:

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

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

谢谢!

【问题讨论】:

标签: javascript jquery html jquery-ui razor


【解决方案1】:
$("#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>

【讨论】:

  • 谢谢伙计,有没有办法可以根据情况更改确认的文本。我正在使用同一个按钮来删除和添加一个帐户,我有一个按钮应该做什么的标志,文本应该像“你确定要删除吗?”如果标志 (StatusId = 0) 或“您确定要添加吗?”如果标志(StatusId = 1)
  • 是的,可以先根据条件创建一个文本并存储在一个变量中,然后将该变量添加到确认窗口中。
  • 我编辑了示例代码。我希望这个示例会有用
  • 完美,非常感谢!
【解决方案2】:

使用标准的 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

【讨论】:

    【解决方案3】:

    像这样:

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

    【讨论】:

      【解决方案4】:
      $("#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
      });
      

      【讨论】:

        猜你喜欢
        • 2011-05-02
        • 2017-04-15
        • 1970-01-01
        • 2016-03-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多