开发者

Reference JSF control's attributes via JavaScript

开发者 https://www.devze.com 2023-01-27 07:52 出处:网络
Is it possible to reference the requ开发者_开发问答ired attribute of a JSF h:inputText control from JavaScript? This is a page that can\'t use AJAX and I\'d like to set the required attribute based on

Is it possible to reference the requ开发者_开发问答ired attribute of a JSF h:inputText control from JavaScript? 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.


I'd like to set the required attribute based on the value in another text field.

Then just check that in the required attribute.

E.g. 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, e.g. "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. The attributes live on another machine to where the Javascript executes.

I think you can write the JSF to generate embedded Javascript that depends on JSF controls. But I suspect that you will need to create and use your own JSF controls to make this happen.

(I'm generalizing from JSPs and JSTL ... but I think that the same principles apply.)


It's not possible to my knowledge. Java Script is executing on the client side, JSF controls are server side.

However, I think you can use ajax push and bind the required attributes value based on the value somebody types in a text field. Take a look at a framework like iceFaces that offers such functionality. But this is moving away from java script.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号