如何在javascript脚本中呈现新页面

时间:2016-08-17 10:18:24

标签: javascript html jsf xhtml

我正在使用XHTML,JSF和JavaScript创建表单,验证信息是否已在h:commandButton中提交到选定字段中,如果已经过验证,则会重定向到另一个页面homepage.xhtml。一切都取决于重定向,我无法开始工作。

在JavaScript函数Validation()中,我尝试过location =" homepage.xhtml",window.location.href =" homepage.xhtml",location.url =&# 34; homepage.xhtml"和其他几个,但似乎没有任何工作。我有一种感觉,我应该有某种声明添加href =" homepage.xhtml"如果Validate()返回true,则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:循环纠正

2 个答案:

答案 0 :(得分:1)

为什么您需要h:commandButton,无论如何您使用简单的javascript验证 h:commandButton呈现为<input type="submit" ../>其使命 提交表格,以便您提交的javascript文件将被提交,您的页面将被刷新,所以如果您需要这样,您必须强制它不提交表单,

然而,通过了解您的需求,您只需要简单<a /><button />,或者只需将type="button"添加到h:commandButton例如:<h:commandButton type="button" .../> < / p>

答案 1 :(得分:0)

你可以使用..     window.location.replace('Your_url'); .. 或者你可以使用..
    window.location.href= 'Your_url'; ..我想还必须有其他功能。如果你想在另一个窗口中打开它,比如弹出窗口,你可以使用.. window.open('your_url');

希望这有帮助!

相关问题