jsf通过javascript从另一个表单按下隐藏的按钮

时间:2014-06-20 13:11:38

标签: javascript jsf-2 primefaces

我有两种形式。形式" form2"有几个按钮。当按下其中一个按钮时,我想按下表格中的隐藏按钮" form1"提交并更新整个表格。

但是当我按下我的表单2中的按钮时,我仍然遇到问题,未提交值。是javascript函数错误还是让Primefaces / JSF更好地做到这一点?

我使用的是Primefaces 5。

我的观点:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui"
    xmlns:c="http://java.sun.com/jsp/jstl/core">

<h:head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</h:head>
<h:body>
<div>
    <h:form id="form1">
        <p:inputText value="#{myBean.myTestInputText}" />
        <!-- Some input elements -->
        <p:commandButton id="myHiddenButton" style="display: none" />
    </h:form>
</div>
<br/>
<div>
    <h:form id="form2">
        <p:commandButton value="Submit Form1"
            onclick="document.getElementById('myHiddenButton').click();"    
        actionListener="#{myBean.doSomeBackendStuff()}"
            update=":form1" />
    </h:form>
</div>
</h:body>
</html>

我的豆子:

package de.vwag.flucs.presentation.admin.boundary;

import java.io.Serializable;

import javax.inject.Named;

import org.apache.myfaces.extensions.cdi.core.api.scope.conversation.ViewAccessScoped;

@ViewAccessScoped
@Named("myBean")
public class MyBean implements Serializable {

    private static final long serialVersionUID = -8923580640140500834L;

    private String myTestInputText = "";

    public String getMyTestInputText() {
        return myTestInputText;
    }

    public void setMyTestInputText(String myTestInputText) {
        this.myTestInputText = myTestInputText;
    }

    public void doSomeBackendStuff() {
        // Do Some Backendstuff!
        System.out.println("He hit me! He entered: " + myTestInputText );
    }
}

1 个答案:

答案 0 :(得分:2)

基本上javascript不起作用,因为隐藏按钮的客户端ID不是myHiddenButton它应该是form1:myHiddenButton