简体   繁体   English

通过JavaScript引用JSF控件的属性

[英]Reference JSF control's attributes via JavaScript

Is it possible to reference the required attribute of a JSF h:inputText control from JavaScript? 是否可以从JavaScript引用JSF h:inputText控件的required属性? This is a page that can't use AJAX and I'd like to set the required attribute based on the value in another text field. 这是一个不能使用AJAX的页面,我想根据另一个文本字段中的值设置required属性。

I'd like to set the required attribute based on the value in another text field. 我想根据另一个文本字段中的值设置必需的属性。

Then just check that in the required attribute. 然后,只需检查required属性。

Eg when you want to let it depend on whether the other field is filled in or not: 例如,何时让它取决于是否填写了其他字段:

<h:inputText value="#{bean.input1}" binding="#{input1}" />
<h:inputText value="#{bean.input2}" required="#{not empty input1.value}" />

Or when it has to be a specific value instead, eg "foo". 或者,当它必须是一个特定值时,例如“ foo”。

<h:inputText value="#{bean.input1}" binding="#{input1}" />
<h:inputText value="#{bean.input2}" required="#{input1.value == 'foo'}" />

Keep it simple. 把事情简单化。

You cannot "access" JSF attributes from Javascript. 您不能从Javascript“访问” JSF属性。 The attributes live on another machine to where the Javascript executes. 这些属性位于执行Javascript的另一台计算机上。

I think you can write the JSF to generate embedded Javascript that depends on JSF controls. 我认为您可以编写JSF来生成依赖JSF控件的嵌入式Javascript。 But I suspect that you will need to create and use your own JSF controls to make this happen. 但是我怀疑您将需要创建并使用自己的JSF控件来实现这一目标。

(I'm generalizing from JSPs and JSTL ... but I think that the same principles apply.) (我从JSP和JSTL进行概括……但是我认为同样的原理也适用。)

It's not possible to my knowledge. 据我所知,这是不可能的。 Java Script is executing on the client side, JSF controls are server side. Java Script在客户端执行,JSF控件在服务器端。

However, I think you can use ajax push and bind the required attributes value based on the value somebody types in a text field. 但是,我认为您可以使用ajax push并根据某人在文本字段中键入的值来绑定所需的属性值。 Take a look at a framework like iceFaces that offers such functionality. 看一下像iceFaces这样的框架,它提供了这样的功能。 But this is moving away from java script. 但这正远离Java脚本。

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

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