JSF Ajax请求不会触发时,与JS客户端验证相结合客户端、JSF、Ajax、JS

2023-09-10 14:26:31 作者:暴躁小姐姐'

我使用JSF 2.0和primefaces。 我有一个网页,一个表单内多个输入和一个按钮,使用AJAX增加了一个新的记录表。一切工作正常。然后,我用JavaScript添加客户端验证。这是命令按钮的code:

I'm using JSF 2.0 and primefaces. I have one page with several inputs inside a form and a button that adds a new record to a table using ajax. Everything works ok. Then I added client side validation using JavaScript. This is the code of the command button:

<p:commandButton value="Add" actionListener="#{reqAbsences.addPreLeaveDemand}"
                 onclick="return validateNewAbs()"
                 update="tableForm inputForm errorForm" />

如果验证失败,它工作正常,并记录不添加任何表。 问题出现时,验证通过:记录被addded,但页面重新加载(阿贾克斯不工作)。如果删除的onclick =返回validateNewAbs()阿贾克斯再次工作。

If validation fails, it works as expected and the record is not added no the table. The problem comes up when validation is passed: the record is addded, but the page is reloaded (ajax not working). if I remove the onclick="return validateNewAbs()" ajax works again.

任何想法,为什么出现这种情况?

Any idea why this happens?

推荐答案

你立即返回真正覆盖默认AJAX 点击事件。而使其返回只在

onclick="if (!validateNewAbs()) return false;"

更好然而使用JSF内置/自定义的验证器只是做验证在服务器端。这样,您就不需要重复验证到两侧和验证将仍然与爵士残疾人工作。

Better is however to just do the validation on the server side using JSF builtin/custom validators. This way you don't need to duplicate validation into the both sides and the validation will still work with JS disabled.