简体   繁体   English

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

[英]how to use javascript to get jsf h:outputText value?

I have a button has action which can set myBackingBean.myString, and onclick event calls js method to alert that value. 我有一个按钮,该按钮具有可以设置myBackingBean.myString的操作,并且onclick事件调用js方法来提醒该值。 I just want to get the value of myString from backing bean by using javascript. 我只想通过使用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 required error. 但我得到Object required错误。 How can i fix this? 我怎样才能解决这个问题? thanks in advance. 提前致谢。

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或作为其容器,其id属性为“ myForm:myOutput”。

Also, the .value javascript accesor is used for input tags, so use inerHTML instead. 此外,.value javascript accesor用于输入标签,因此请改用inerHTML。

You need not always to have a hidden field to access the Bean Property. 您不必总是具有隐藏字段来访问Bean属性。 You can do it as below. 您可以按照以下步骤进行操作。

<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 onComplete attribute as below. 但是,如果要在单击按钮时更改' myString '的值,然后要显示新值,则应使用<a4j:commandButton/> ,它的onComplete属性如下。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM