问题
I have a jsf code like this:
<h:inputText id="emailText" styleClass="emailAddress" value="{bean.emailText}"/>
I am adding the style emailAddress to use it in jquery for restricting the user from copying and pasting text in textbox like below:
jQuery(".emailAddress").bind("copy paste",function(e) {
e.preventDefault();
});
But on click of Submit, when the text field is validated and if the text entered is in improper format, bean side, I am adding another style class to the textbox to highlight it in red color like:
((HtmlInputText) getUIComponent(context, component, "emailText")).setStyleClass("error");
Now the problem is the newly added style class is overriding the existing emailAddress class which was used for restricting copy,paste in jquery. I am using only class approach in jquery as I want to apply the restricting functionality to all the email address fields through out the application. So, please help me how to handle the situation. I mean how to avoid the overriding of the existing style class when we call setStyClass(). Thanks.
回答1:
You can try another approach :
first create method in bean which returns boolean value for text-field validation.
use that method in styleClass attribute to add class "error" when it returns false.
styleClass="emailAddress #{beanName.isvalidate eq true ? 'no-error' : 'error'}"
on submit button don't forget to update h:inputText [ use update="emailText" for submit button ]
来源:https://stackoverflow.com/questions/29163189/setstyleclass-method-is-overriding-the-already-defined-styleclass