用户不会显示 JQuery 模式对话框确认

JQuery modal dialog confirmation doesn't appear to the user

本文关键字:模式 对话框 确认 JQuery 显示 用户      更新时间:2023-09-26

我正在开发一个基于 ASP.NET 并用C#编码的WebForms应用程序。此应用程序执行 CRUD 操作,每次用户想要执行操作时,我都需要向客户端显示确认消息。我决定创建一个 jQuery 函数,该函数接收窗口的标题、要向用户显示的消息以及表示操作的按钮。

这是我的Javascript函数:

var _confirm = false;
function Confirm(str, strtitle,button) {
  e.preventDefault();
  if (!strtitle) strtitle = 'Mensaje de error';
  $('#dialog').show();
  $('#dialog').html(str);
  $("#dialog").dialog({
    autoOpen: true,
    draggable: false,
    resizable: false,
    modal: true,
    title: strtitle,
    width: 350,
    height: 180,
    show: "slide",
    hide: "puff",
    buttons: {
      "No": function () {
        jQuery(this).dialog("close");
      },
      "Yes": function () {
        jQuery(this).dialog("close");
        _confirm = true;
        button.click();
      }
    },
    close: function () {
      jQuery(this).remove();
    }
  });
  return false;
}

这是 ASP 按钮:

<asp:button id="btnOk" onclick="btnDGV_Click" 
            onclientclick="Confirm('Desea eliminar el registro', 'Confirmacion', this);" 
            runat="server" text="Eliminar Registro">
</asp:button>

服务器端点击的事件代码(暂时只是确认消息):

protected void btnDGV_Click(object sender, EventArgs e) {
  ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Registro eliminado exitosamente !!!')", true);
}

问题是当我单击该按钮时,服务器端消息始终出现,并且永远不会显示jQuery对话框。可能是什么问题?

修改你的代码: onclientclick="Confirm('Desea eliminar el registro', 'Confirmacion', this);return;" 这会阻止按钮进行回发

并修改: "Yes": function () { jQuery(this).dialog("close"); __doPostBack('<%=btnOk.ClientID%>', '') button.click(); } 如果单击"是",则此代码将进行回发和调用在按钮的 OnClick 属性上指定的方法

这里有一些事情需要检查,应该会让你明白:

  1. 检查客户端上的 asp:Button 呈现方式(用于您的启发)
  2. 检查您的客户端单击是否会阻止该元素发布到服务器。您可能希望在此处返回确认函数的结果。
  3. 检查确认函数的顺序。您正在尝试在初始化之前显示对话框。