开发者

JSF / RichFaces commandButton called by JavaScript does not redirect

开发者 https://www.devze.com 2023-04-06 09:25 出处:网络
I have a javascript function 开发者_如何学Cto run h: or a4j: commanButton. When javascript function is called, action button runs action but after that page is not redirected.

I have a javascript function 开发者_如何学Cto run h: or a4j: commanButton. When javascript function is called, action button runs action but after that page is not redirected. I am using Seam 2.2 and RichFaces 3.3.3 What is the problem here? Thanks.

  function submitForm(){
        document.getElementById('myForm:save').click(); 

        // does not redirect.
        //document.getElementById('myForm').submit();
        }  

Even if I use submit() page is not redirected.

Form:

<h:form id="myForm">
//some fields

<h:commandButton id="save" value="Save"
                         action="#{personHome.persist}" />

</h:form>


To the point, the following should work for <h:commandButton>:

document.getElementById('myForm:save').click(); 

and when the button is the first button of the form in question:

document.getElementById('myForm').submit();

You only need to ensure that the generated client ID is exactly the same as the ID which you're specifying in getElementById(). If the <h:form> is by itself nested in another UINamingContainer component, then the ID will be prepended with its ID. Rightclick page in browser and View Source to be sure.

As to the concrete problem, perhaps you've attached this function to another button which in turn also submits some form by itself which will result in a race condition of two requests. You should then return false; from or after the function to block the caller's default action. E.g.

<h:commandButton onclick="return submitForm();" />

with

function submitForm(){
    // ...

    return false;
}  
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号