【发布时间】:2016-12-23 22:46:00
【问题描述】:
我正在使用 XHTML、JSF 和 JavaScript 创建一个表单,验证信息是否已提交到 h:commandButton 中的 onclick 选定字段中,如果验证通过,则重定向到不同的页面 homepage.xhtml。一切正常,我无法工作。
在 JavaScript 函数 Validation() 中,我尝试了 location="homepage.xhtml"、window.location.href="homepage.xhtml"、location.url="homepage.xhtml" 和其他一些,但没有似乎工作。我有一种感觉,如果 Validate() 返回 true,我应该有某种声明,将 href="homepage.xhtml" 添加到 h:commandButton,但我不确定如何做到这一点。
非常感谢任何帮助。我在下面添加了相关代码。
按钮:
<h:commandButton class="btn btn-warning" value="Continue" onclick="Validation()"/>
验证
function Validation() {
var nameCheck = document.getElementById('formdiv:cardName');
var numCheck = document.getElementById('formdiv:cardNumber');
var expCheck = document.getElementById('formdiv:expDate');
console.log(nameCheck.value);
console.log(numCheck.value);
console.log(expCheck.value);
var variablesToCheck = [nameCheck, numCheck, expCheck];
for(i=0; i < variablesToCheck.length; i++){
if(variablesToCheck[i].value == null || variablesToCheck[i].value == ""){
alert("Fields marked with a * must be completed");
return false;
}
}
// This is where the redirection needs to go, I think...
return true;
}
编辑:刚刚注意到 if else 语句在逻辑上是不正确的,但在语法上它不应该有所作为。 else 部分需要是循环外没有条件的语句;这段代码只是在它检查的字段中有内容时尝试重定向,而不是在所有字段都有内容时重定向。
编辑 2:循环更正
【问题讨论】:
-
你不应该在你的 for 循环之后重定向吗?
-
@JasperdeVries 是的,我自己也注意到了。现在修改了
-
你为什么不在输入上使用 JSF
required="true"?另见stackoverflow.com/q/4013410
标签: javascript html jsf xhtml