代码之家  ›  专栏  ›  技术社区  ›  Grant Wagner

如何在执行jsf<h:commandlink>操作之前执行javascript?[复制品]

  •  11
  • Grant Wagner  · 技术社区  · 16 年前

    这个问题已经有了答案:

    如果你有JSF <h:commandLink> (使用 onclick AN事件 <a> 要提交当前表单),如何在执行操作之前执行javascript(例如请求删除确认)?

    8 回复  |  直到 9 年前
        1
  •  14
  •   iordan    14 年前

    可以这样简化

    onclick="return confirm('Are you sure?');"
    
        2
  •  7
  •   Grant Wagner    16 年前
    <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>
    

    其他javascript操作(如验证表单输入等)可以通过替换对 confirm() 调用另一个函数。

        3
  •  7
  •   Hanynowsky    13 年前

    这对我很有用:

    <h:commandButton title="#{bundle.NewPatient}" action="#{identifController.prepareCreate}"  
                                                 id="newibutton" 
                                                 onclick="if(confirm('#{bundle.NewPatient}?'))return true; else return false;" 
                                                 value="#{bundle.NewPatient}"/>
    
        4
  •  1
  •   noah    16 年前

    您仍然可以使用onclick。JSF render kit specification (请参见编码行为)描述链接应该如何处理它。以下是重要的部分(它为onclick呈现的效果):

    var a=function(){/*your onclick*/}; var b=function(){/*JSF onclick*/}; return (a()==false) ? false : b();
    

    因此,您的函数不会被传递给事件对象(无论如何,这不是可靠的跨浏览器),但是返回true/false会使提交短路。

        5
  •  0
  •   user7094    16 年前

    在JSF1.2中,可以指定onclick事件。

    另外,其他库,如 MyFaces IceFaces 实现“onclick”处理程序。

    那么你需要做的只是:

    <h:commandLink action="#{bean.action}" onclick="if(confirm('Are you sure?')) return false;" />
    

    注意:你不能只做 return confirm(...) 因为这将阻止onclick事件中的其他javascript发生,这将有效地阻止您的操作发生,无论用户返回什么!

        6
  •  0
  •   Victor    16 年前

    如果要在发布表单之前执行某个操作,例如确认,请尝试提交表单事件。类似:

    myform.onsubmit = function(){confirm("really really sure?")};
    
        7
  •  0
  •   Creigh    16 年前

    这对我没用,

     onclick="if(confirm('Are you sure?')) return false;" />
    

    但这样做了

    onclick="if(confirm(\"Are you sure?\"))return true; else return false;"
    
        8
  •  0
  •   user562189    14 年前
    var deleteClick;
    var mess="xxx";
    function assignDeleteClick(link) {
        if (link.onclick == confirmDelete) {
            return;
        }
        deleteClick = link.onclick;
        link.onclick = confirmDelete;
    }
    
    
    function confirmDelete() {
        var ans = confirm(mess);
        if (ans == true) {
            return deleteClick();
        } else {
            return false;
        }
    } 
    

    对JSF1.1使用此代码。