Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JSF-我如何实现JavaScript;你确定吗?”;提示输入一个<;h:命令按钮>;_Java_Javascript_Jsf_Myfaces - Fatal编程技术网

JSF-我如何实现JavaScript;你确定吗?”;提示输入一个<;h:命令按钮>;

JSF-我如何实现JavaScript;你确定吗?”;提示输入一个<;h:命令按钮>;,java,javascript,jsf,myfaces,Java,Javascript,Jsf,Myfaces,在我的JSF1.2WebApp中,我有一个带有的页面,该页面调用支持bean上的操作方法。此操作将导致删除/替换数据库中的数据,因此我希望避免用户意外单击命令按钮的任何情况 我想使用JavaScript实现一个简单的“确定吗?”提示,并使用“是/否”或“确定/取消”选项。我对JavaScript不是很在行,而且我以前从未将JavaScript与JSF混合使用过。有人能提供一个代码片段来告诉我如何实现这个吗 下面是我的JSP页面中声明命令按钮的部分: <h:commandButton

在我的JSF1.2WebApp中,我有一个带有
的页面,该页面调用支持bean上的操作方法。此操作将导致删除/替换数据库中的数据,因此我希望避免用户意外单击命令按钮的任何情况

我想使用JavaScript实现一个简单的“确定吗?”提示,并使用“是/否”或“确定/取消”选项。我对JavaScript不是很在行,而且我以前从未将JavaScript与JSF混合使用过。有人能提供一个代码片段来告诉我如何实现这个吗

下面是我的JSP页面中声明命令按钮的部分:

<h:commandButton 
    id="commandButtonAcceptDraft"
    title="#{bundle.tooltipAcceptDraft}" 
    action="#{controller.actionReplaceCurrentReportWithDraft}" 
    image="/images/checkmark.gif">
</h:commandButton>

我不确定您需要听什么事件(我假设是onclick),因为我从未使用过JSF。一般来说,这应该是可行的

var element = document.getElementById('commandButtonAcceptDraft');

element.onclick = function(e){
  return confirm('Are you sure etc...');
};

您可以将javascript添加到按钮的onclick中

<h:commandButton  
    id="commandButtonAcceptDraft" 
    title="#{bundle.tooltipAcceptDraft}"  
    action="#{controller.actionReplaceCurrentReportWithDraft}"  
    onclick="return confirm('Are you sure?')"
    image="/images/checkmark.gif"> 
</h:commandButton> 

这应该行得通。理想情况下,它应该位于java脚本文件中

<h:commandButton 
    id="commandButtonAcceptDraft"
    title="#{bundle.tooltipAcceptDraft}" 
    action="#{controller.actionReplaceCurrentReportWithDraft}" 
    image="/images/checkmark.gif"
     onclick="if (!confirm('Are you sure?')) return false">
</h:commandButton>

使用JavaScript函数。它返回一个
布尔值。如果返回false,则按钮的默认操作将被阻止,否则将继续

<h:commandButton onclick="return confirm('Are you sure?')" />


因为它已经返回了
boolean
,所以如果其他答案建议使用
a,则完全没有必要将其包装在
中。

如果您想使用javascript获取ElementById,那么我相信您需要包含表单名称。在JSF中,元素的实际ID类似于“formName:commandButtonAcceptDraft”,在外部脚本中添加事件比在内联脚本中添加事件更有优势。这确实有效,但如果您需要将其添加到大量项目中,或者将来需要更改消息,则无法轻松编辑它。@CoolBeans解决方案在我的情况下是正确的,否则操作将重新加载页面。我在用我的脸和脸BootsFaces@MitchBroadhead然后,JSF impl或组件库以错误的方式生成onclick属性。只需查看生成的HTML输出,并向负责的开发人员报告问题。我在BootsFaces提交了一个bug,Stephan现在已经解决了这个问题。这是我的正确解决方案@BalusC解决方案导致重新加载带有MyFaces和BootsFaces的页面。不知道为什么。
<h:commandButton  
    id="commandButtonAcceptDraft" 
    title="#{bundle.tooltipAcceptDraft}"  
    action="#{controller.actionReplaceCurrentReportWithDraft}"  
    onclick="return confirm('Are you sure?')"
    image="/images/checkmark.gif"> 
</h:commandButton> 
<h:commandButton 
    id="commandButtonAcceptDraft"
    title="#{bundle.tooltipAcceptDraft}" 
    action="#{controller.actionReplaceCurrentReportWithDraft}" 
    image="/images/checkmark.gif"
     onclick="if (!confirm('Are you sure?')) return false">
</h:commandButton>
<h:commandButton onclick="return confirm('Are you sure?')" />