如何在执行JSF <h:commandLink>操作之前执行Javascript?

Gra*_*ner 11 javascript jsf

如果您有一个JSF <h:commandLink>(使用a的onclick事件<a>来提交当前表单),那么在执行操作之前如何执行JavaScript(例如请求删除确认)?

小智 14

可以像这样简化

onclick="return confirm('Are you sure?');"
Run Code Online (Sandbox Code Playgroud)


Gra*_*ner 7

<h:commandLink id="myCommandLink" action="#{myPageCode.doDelete}">
    <h:outputText value="#{msgs.deleteText}" />
</h:commandLink>
<script type="text/javascript">
if (document.getElementById) {
    var commandLink = document.getElementById('<c:out value="${myPageCode.myCommandLinkClientId}" />');
    if (commandLink && commandLink.onclick) {
        var commandLinkOnclick = commandLink.onclick;
        commandLink.onclick = function() {
            var result = confirm('Do you really want to <c:out value="${msgs.deleteText}" />?');
            if (result) {
                return commandLinkOnclick();
            }
            return false;
        }
    }
}
</script>
Run Code Online (Sandbox Code Playgroud)

其他Javascript操作(如验证表单输入等)可以通过将调用替换为confirm()对另一个函数的调用来执行.


Han*_*sky 7

这对我有用:

<h:commandButton title="#{bundle.NewPatient}" action="#{identifController.prepareCreate}"  
                                             id="newibutton" 
                                             onclick="if(confirm('#{bundle.NewPatient}?'))return true; else return false;" 
                                             value="#{bundle.NewPatient}"/>
Run Code Online (Sandbox Code Playgroud)

  • 这可以简化为`onclick ="return confirm('你确定吗?')"`.不必检查布尔值以返回布尔值.直接退货吧. (10认同)