如何使用 javascript 获取 jsf h:outputText 值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10234613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 09:08:55  来源:igfitidea点击:

how to use javascript to get jsf h:outputText value?

javascriptjsf-2backing-beans

提问by Celia

I have a button has action which can set myBackingBean.myString, and onclick event calls js method to alert that value. I just want to get the value of myString from backing bean by using javascript.

我有一个按钮具有可以设置 myBackingBean.myString 的操作,并且 onclick 事件调用 js 方法来提醒该值。我只想通过使用 javascript 从支持 bean 中获取 myString 的值。

I have a hidden output which has value from backing bean:

我有一个隐藏的输出,它具有来自支持 bean 的价值:

h:outputText id="myOutput" rendered="false" value="#{myBackingBean.myString}" 

then I need to alert this value in javascript fxn which triggered by a button:

然后我需要在由按钮触发的 javascript fxn 中提醒这个值:

function myFunction() {

var outPut= document.getElementById("myForm:myOutput").value;

...

}

but i got Object requirederror. How can i fix this? thanks in advance.

但我有Object required错误。我怎样才能解决这个问题?提前致谢。

回答by Floydian

Make you sure that the h:outputText is rendered (rendered="false" could just not add it to the DOM. If it does not render, it can't be accessed. If you need it hidden, use h:inputHidden instead).

确保 h:outputText 已呈现(rendered="false" 无法将其添加到 DOM。如果它不呈现,则无法访问。如果您需要隐藏它,请改用 h:inputHidden) .

Then make sure that it renders an HTML tag as or acting like a container with the id attribute as "myForm:myOutput".

然后确保它将 HTML 标记呈现为或充当具有 id 属性为“myForm:myOutput”的容器。

Also, the .value javascript accesor is used for input tags, so use inerHTML instead.

此外,.value javascript accesor 用于输入标签,因此请改用 inerHTML。

回答by prageeth

You need not always to have a hidden field to access the Bean Property. You can do it as below.

您不必总是有一个隐藏字段来访问 Bean 属性。您可以按照以下方式进行。

<h:commandButton value="Show" onclick="alert('#{myBackingBean.myString}');"/>

But if you want to change the value of 'myString' when you click the button and then you want to display the new value you should use a <a4j:commandButton/>and it's onCompleteattribute as below.

但是,如果您想在单击按钮时更改“ myString”的值,然后想显示新值,则应使用 a<a4j:commandButton/>并且它的onComplete属性如下所示。

<a4j:commandButton value="Change" action="#{myBackingBean.changeString()}" oncomplete="alert('#{myBackingBean.myString}');" />