我有一个带有不同输入文本的表单,我想在客户端验证我的表单,所以我编写了一个 java 脚本函数,表单的验证基于定义的 bean(userManagement.update)中的属性,这意味着如果 userManagement.update 为 true,则不需要这些输入文本,但如果 userManagement.update 为 false,则需要这些输入文本。问题是 javascript 函数中的 userManagement.update 没有改变。
<h:outputLabel for="pwd1" value="* #{i18n['password']}: "
rendered="#{!userManagement.update}" />
<h:outputLabel for="pwd1" value=" #{i18n['password']}: "
rendered="#{userManagement.update}" />
<p:password id="pwd1" value="#{userManagement.user.password}"
match="pwd2" label="Password 1"
required="#{!userManagement.update}"
requiredMessage="#{i18n['password_required']}"
validatorMessage="#{i18n['password_match_repeat_password']}" />
<span style="color: red" id="emptyPasswordError" />
<h:outputLabel for="pwd2" value="* #{i18n['repeat_password']}: "
rendered="#{!userManagement.update}" />
<h:outputLabel for="pwd2" value=" #{i18n['repeat_password']}: "
rendered="#{userManagement.update}" />
<p:password id="pwd2" value="#{userManagement.user.password}"
required="#{!userManagement.update}"
requiredMessage="#{i18n['repeat_password_required']}" />
<span style="color: red" id="emptyRepeatPasswordError" />
<p:commandButton value="#{i18n['update']}"
actionListener="#{userManagement.update}">
</p:commandButton>
<script type="text/javascript">
function validateForm() {
if ($("#addUserform:pwd1").val() === "")
{
if("#{!userManagement.update}"){
$("#emptyPasswordError").text("#{i18n['password_required']}");
validation = false;
}
}
else{
$("#emptyPasswordError").text("");
}
if ($("#addUserform:pwd2").val() === "")
{
alert("#{!userManagement.update}");
if("#{!userManagement.update}"){
$("#emptyRepeatPasswordError").text( "#{i18n['repeat_password_required']}");
validation = false;
}
}
else{
$("#emptyRepeatPasswordError").text("");
}
return validation;
}
</script>