我使用XHTML,JSF和JavaScript创建表单,验证信息已提交到选定字段中的onclick中:commandButton,如果验证,则重定向到其他页面homepage.xhtml。一切工作到重定向,我无法去工作。在JavaScript函数Validation()中,我尝试了位置=“homepage.xhtml”,window.location.href =“homepage.xhtml”,location.url =“homepage.xhtml”和其他几个,但是,似乎没有任何工作。我有一种感觉,我应该有某种类型的声明,如果Validate()返回true,那么会向h:commandButton添加href =“homepage.xhtml”,但我不确定如何操作。如何在JavaScript脚本中渲染新页面
任何帮助,非常感谢。我在下面添加了相关的代码。
按钮:
<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;
}
编辑:只是注意到如果else语句是不正确逻辑,但在语法上不应该有所作为。 else部分需要在没有条件的循环之外进行声明;这个代码只是尝试当它检测领域中拥有的东西,而不是在所有领域有一些重定向
编辑2:循环修正
不应该在for循环之后重定向吗? –
@JasperdeVries是的,我注意到了我自己。现在修改它 – CGurrell
http://stackoverflow.com/questions/15515946/how-to-validate-a-field-in-jsf –