【问题标题】:jsf Press a hidden Button by javascript from another formjsf 通过 javascript 从另一个表单按下隐藏按钮
【发布时间】:2014-08-11 06:16:09
【问题描述】:

我有两种形式。表单“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 jsf-2 primefaces


    【解决方案1】:

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

    【讨论】:

    • 感谢您提供错误客户 ID 的提示。现在它正在使用js。但是primefaces方式仍然不起作用。如果我按下提交按钮,则 inputtext 字段不会提交给 bean。
    • 显然我写错了,看最后一个答案stackoverflow.com/questions/9738871/…
    • 或者你的方式我觉得很好
    猜你喜欢
    • 1970-01-01
    • 2015-07-04
    • 1970-01-01
    • 2015-01-07
    • 2011-12-10
    • 1970-01-01
    • 2018-01-01
    • 1970-01-01
    • 2013-03-28
    相关资源
    最近更新 更多