【问题标题】:Use Ajax and Confirm message for delete使用 Ajax 和确认消息进行删除
【发布时间】:2023-04-03 23:46:02
【问题描述】:

我想在单击删除 ImageButton 时收到确认消息。如果用户选择“确定”,则删除完成,否则如果单击“取消”,则没有任何反应。

<asp:ImageButton ID="ImageButton1" runat="server"  ImageUrl="~/images/forDesign/delete.png"   OnClientClick="ConfirmDelete()"  />

<script type="text/JavaScript" 
src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
    function ConfirmDelete() {
        var x = confirm("Are you sure you want to delete?");
        if (x)
            $.ajax({
                type: "get",
                url: "SearchTicket.aspx/Delete",
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                success: function (response) {
                    alert(response.status);
                },
                error: function () {
                    alert("error");
                }
            });           
    }

在我后面的代码中:

 [WebMethod]
public void Delete()
{
    string code = DetailsViewCodeTicket.Rows[2].Cells[1].Text.ToString();
    Ticket.TicketCusDelete(code);
    DetailsViewCodeTicket.DataBind();
}

但是当我单击 ImageButton 时出现此错误:

我该怎么做?

【问题讨论】:

    标签: javascript jquery asp.net


    【解决方案1】:

    您应该首先在点击时启动您的灯箱……然后,当用户确认您触发您的 ajax 调用时……

    这里是一个例子:http://jsfiddle.net/leojavier/976oxwmk/

    <button>remove</button>
    

    js

    $('button').on('click', function(){
    
        var confirmation = confirm("are you sure you want to remove the item?");
    
        if (confirmation) {
         // execute ajax
            alert('removed');
        }
    })
    

    【讨论】:

      【解决方案2】:

      我认为你只需要添加大括号:

      if(x){
      ajax code ....
      }
      

      所以这将是整体代码:

      <asp:ImageButton ID="ImageButton1" runat="server"  ImageUrl="~/images/forDesign/delete.png"   OnClientClick="ConfirmDelete()"  />
      <script type="text/JavaScript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
      <script type="text/javascript">
          function ConfirmDelete() {
              var x = confirm("Are you sure you want to delete?");
              if (x) {
                  $.ajax({
                      type: "get",
                      url: "SearchTicket.aspx/Delete",
                      contentType: 'application/json; charset=utf-8',
                      dataType: 'json',
                      success: function (response) {
                          alert(response.status);
                      },
                      error: function () {
                          alert("error");
                      }
                  });
              }
          }
      </script>
      

      【讨论】:

      • 感谢您分享原始发帖人问题的答案。请注意正确格式化答案。如需更多帮助,请参阅以下降价指南:stackoverflow.com/editing-help
      猜你喜欢
      • 2015-09-29
      • 2020-02-26
      • 1970-01-01
      • 1970-01-01
      • 2020-03-09
      • 2013-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多