用javascript从另一个表单中按下隐藏的按钮

jsf Press a hidden Button by javascript from another form

本文关键字:隐藏 按钮 javascript 另一个 表单      更新时间:2023-09-26

我有两个表单。表单"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 );
    }
}

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