【问题标题】:jQuery UI confirm dialog not returns true/falsejQuery UI 确认对话框不返回真/假
【发布时间】:2012-07-10 02:53:48
【问题描述】:

我有 jQuery UI 确认对话框:

function fnComfirm(title, content) {
    $("#dialog:ui-dialog").dialog("destroy");
    $("#dialog-confirm p").html(content);
    $("#dialog-confirm").dialog({
        title: title,
        resizable: false,
        height: 200,
        width: 486,
        modal: true,
        buttons: {
            "OK": function() {
                $( this ).dialog("close");
                return true;
            },
            Cancel: function() {
                $( this ).dialog("close");
                return false;
            }
        }
    });
}

由 JSF2 html 调用:

<a4j:commandButton action="#{userBean.makeSomething()}"  type="button" onclick="if (fnValidateUsersList()) {return fnComfirm('Delete User', 'Bla bla')}" />

但我无法从此函数中获取真/假值。我在这个网站上看到了很多关于它的问题,尝试了所有这些,但仍然没有成功。

更新: &lt;a4j:commandButton&gt;的输出:

<input type="button" value="Make Something" onclick="jsf.util.chain(this,event,&quot;if (fnValidateUsersList()) {return fnComfirm('Delete User', 'Bla bla')}&quot;,&quot;RichFaces.ajax(\&quot;frm:j_idt25\&quot;,event,{\&quot;incId\&quot;:\&quot;1\&quot;} )&quot;);return false;" name="frm:j_idt25" id="frm:j_idt25">

【问题讨论】:

    标签: jquery-ui jsf-2 jquery-ui-dialog


    【解决方案1】:

    这是我使用的一种方法(您可以将总体思路作为示例)

    你需要两个按钮

    第一个将隐藏并在确认对话框中单击 Yes 后调用该按钮,此隐藏按钮将调用服务器端方法并执行render 使用 f:ajax

    <h:commandButton id="myHiddenButtonID" value="DeleteHiddenButton" action="#{userBean.makeSomething()}" style="display:none">
        <f:ajax render="@form" ></f:ajax>
    </h:commandButton>
    

    第二个按钮将打开对话框,此按钮还将使用execute="@form" 将表单提交到服务器(如果您在表格中有选择列(选择表格中的多个列并单击按钮删除),例如,您要在服务器 bean 中更新)

    <h:commandButton value="Delete">
        <f:ajax execute="@form" onevent="openDialogFunc()"></f:ajax>
    </h:commandButton>
    

    现在到js函数的实现

    function openDialogFunc(data){
        if (data.status === 'success') {
            $('#dialog').dialog({
                        title: title,
                        resizable: false,
                        height: 200,
                         width: 486,
                         modal: true,
             buttons: {
                     "Ok": function() { 
                         $("#myHiddenButtonID").click();
                         $(this).dialog("close"); 
                     }, 
                     "Cancel": function() { 
                         $(this).dialog("close"); 
                     } 
                 }
             });
        }
    }
    

    请注意,只有在单击“确定”对话框按钮时,您的隐藏按钮 $("#myHiddenButtonID").click(); 才会执行,否则不会发生任何事情...

    从我过去回答过的类似问题中得到这个How to implement JQuery confirm dialog with JSF

    【讨论】:

      【解决方案2】:
      function fnComfirm(title, content) {
          $("#dialog:ui-dialog").dialog("destroy");
          $("#dialog-confirm p").html(content);
          $("#dialog-confirm").dialog({
              title: title,
              resizable: false,
              height: 200,
              width: 486,
              modal: true,
              buttons: {
                  "OK": function() {
                      //do whatever you need here, i.e. form post
                      alert('ok!');
                  },
                  Cancel: function() {
                      $( this ).dialog("close");
                      return false;
                  }
              }
          });
      }
      

      【讨论】:

      • 对不起,我需要调用一些Java函数,它必须在JSF代码中,而不是在这里。
      【解决方案3】:

      发生这种情况是因为函数执行并调用对话框插件并完成,而后者又被执行并且按钮上的返回不会影响事件。

      克服它的最好方法是创建一个根据按下的按钮执行的回调函数:

      function fnComfirm(title, content, callbackOK, callbackCancel) {  
          $("#dialog:ui-dialog").dialog("destroy");  
          $("#dialog-confirm p").html(content);  
          $("#dialog-confirm").dialog({  
              title: title,  
              resizable: false,  
              height: 200,  
              width: 486,  
              modal: true,  
              buttons: {  
                  "OK": function() {  
                      $( this ).dialog("close"); 
                      if (typeof callbackOK == function) {
                          callbackOK();
                      } else if (callbackOK) {
                          window.location.href = callbackOK;
                      }
                      return true;  
                  },  
                  Cancel: function() {  
                      $( this ).dialog("close");
                      if (typeof callbackCancel == function) {
                          callbackCancel();
                      } else if (callbackCancel) {
                          window.location.href = callbackCancel;
                      }
      
                      return false;  
                  }  
              }  
          });
          return false;
      }  
      

      那么你可以这样称呼它:

      ... onclick="if (fnValidateUsersList()) {return fnComfirm('Delete User', 'Bla bla', this.href)}" 
      

      【讨论】:

      • @udar_molota 在这种情况下,优点是您可以使用函数或 URL 作为回调参数。
      • 这很令人困惑:你用 3 个参数调用函数,而函数本身有 4 个参数。而且我不使用重定向,我正在调用一些 Java 函数。
      • 在 javascript 中,所有参数都是可选的,我在函数内部进行检查以证明该值存在,然后再对其进行任何操作。您可以选择使用重定向或通知要为 OK 或 Cancel 执行的函数。
      • 我能以某种方式从这个函数返回布尔值吗?我需要它在按钮中。
      • 执行对话框的分隔上下文不允许这样做。 jsf2 控件的输出 HTML 是什么?一个带有 onclick 事件的按钮?输出上的 action 属性会发生什么?
      【解决方案4】:

      返回是在嵌套函数中的确定和取消按钮的上下文中,您可以尝试回调:

      function fnComfirm(title, content,onSusses,onCancel) {
          $("#dialog:ui-dialog").dialog("destroy");
          $("#dialog-confirm p").html(content);
          $("#dialog-confirm").dialog({
              title: title,
              resizable: false,
              height: 200,
              width: 486,
              modal: true,
              buttons: {
                  "OK": function() {
                      $( this ).dialog("close");
                      onSusses();
                  },
                  Cancel: function() {
                      $( this ).dialog("close");
                      onCancel();
                  }
              }
          });
      }
      

      然后调用

      fnComfirm(title,content,function(){alert('Ok')},function(){alert('cancel');}})
      

      【讨论】:

      • 别忘了检查回调函数是否有效。如果没有,如果其中一个被省略,这将导致错误。
      • 还是没有运气。我认为原因是javascript是异步的,并且按钮的默认操作不等待函数的回答。
      【解决方案5】:

      我不太了解 JS,但我的猜测是 return false 在嵌套函数中返回?!你想做的是这个;

      function fnComfirm(title, content) {
      var returnVar;
      $("#dialog:ui-dialog").dialog("destroy");
      $("#dialog-confirm p").html(content);
      $("#dialog-confirm").dialog({
          title: title,
          resizable: false,
          height: 200,
          width: 486,
          modal: true,
          buttons: {
              "OK": function() {
                  $( this ).dialog("close");
                  returnVar = true;
              },
              Cancel: function() {
                  $( this ).dialog("close");
                  returnVar = false;
              }
          }
      });
      return returnVar;
      }
      

      就像我说的,我不是 Javascript 方面的专家,但这就是我认为的问题 :)

      【讨论】:

      • No ... 在 OK 和 Cancel 函数的上下文中,返回 true/false 告诉 jQuery/javascript 不要冒泡事件。
      • 好吧,就像我说的我不是 Javascript(或 JQuery)方面的专家,这只是我的预期。
      • 我试过了,但它不起作用......它总是返回 false。
      猜你喜欢
      • 1970-01-01
      • 2011-04-03
      • 1970-01-01
      • 1970-01-01
      • 2010-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多