简体   繁体   English

带有h:commandButton的动态消息内容的确认对话框

[英]Confirmation dialog with dynamic message content for h:commandButton

I am trying to create a confirmation dialog for a commandButton in JSF. 我正在尝试为JSF中的commandButton创建一个确认对话框。 The message I want to display is "Are you sure you want to deploy to #{deployBean.environmentName}?", where #{deployBean.environmentName} refers to the selected value of h:selectOneMenu. 我要显示的消息是“确定要部署到#{deployBean.environmentName}吗?”,其中#{deployBean.environmentName}表示h:selectOneMenu的选定值。

I used plain JavaScript in the 'onClick' parameter of h:commandButton to do this. 我在h:commandButton的'onClick'参数中使用了纯JavaScript来执行此操作。 The XHTML code is as follows: XHTML代码如下:

<h:commandButton id="deploy" 
value="Deploy" 
action="#{deployBean.deployButtonAction}" 
disabled="#{(!deployBean.checkDeploy and (deployBean.stageComplete or deployBean.stageRunning)) or (deployBean.stageStart)}" 
onclick="return confirm('Are you sure you want to deploy to #{deployBean.environmentName}?');" > 
    <f:ajax event="keyup" render="status output detail"/>
</h:commandButton>

But the environment name does not get displayed in the confirmation dialog box. 但是环境名称不会显示在确认对话框中。

代码输出

Please let me know what I'm missing in my code. 请让我知道我的代码中缺少的内容。 Or please let me know if there is a better way to do this. 或者,如果有更好的方法,请告诉我。

Hmmm, that won't work because the plain JavaScript bit you mentioned. 嗯,那是行不通的,因为您提到的是普通的JavaScript =) You see, JavaScript does not know anything about that #{deployBean.environmentName} syntax -- this EL expression is evaluated in the server only once, before the page loads, and that's it. =)您会看到,JavaScript对#{deployBean.environmentName}语法#{deployBean.environmentName} -在页面加载之前,此EL表达式仅在服务器中被评估一次,仅此而已。

Even if you do an AJAX update in your h:selectOneMenu trying to make sure the value in deployBean is set when the button is clicked, the onclick will be evaluated only in the client side. 即使您在h:selectOneMenu进行AJAX更新,试图确保在单击按钮时设置deployBean的值, onclick也只会在客户端进行评估。 So, you can do what you want with plain JavaScript & DOM manipulation, maybe something like this: 因此,您可以使用简单的JavaScript和DOM操作来完成所需的操作,也许是这样的:

onclick="return confirm('Are you sure you want to deploy to ' +
         document.getElementsByClassName('environment-name')[0].value + '?');"

And give your h:selectOneMenu a styleClass="environment-name" . 并给您的h:selectOneMenu一个styleClass="environment-name"

That should do what you want. 那应该做你想要的。

Of course, if you happen to use jQuery in your app, you can make it a bit shorter: 当然,如果您碰巧在应用程序中使用jQuery,则可以使其更短一些:

onclick="return confirm('Are you sure you want to deploy to ' +
         $('.environment-name').val() + '?');"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM