【问题标题】:How to render a new page in a javascript script如何在 javascript 脚本中呈现新页面
【发布时间】: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:循环更正

【问题讨论】:

标签: javascript html jsf xhtml


【解决方案1】:

为什么你需要h:commandButton 无论如何你正在使用简单的javascript 验证 h:commandButton 被渲染为 &lt;input type="submit" ../&gt; 它的使命是 提交表单,这样您编写表单的任何 javascript 都将被提交并且您的页面将被刷新,所以如果您需要这种方式,您必须强制它不提交表单,

但是,从了解您的需求来看,您所需要的只是简单的 &lt;a /&gt;&lt;button /&gt; ,或者您可以将 type="button" 添加到您的 h:commandButton 例如:&lt;h:commandButton type="button" .../&gt;

【讨论】:

  • 我添加了 type="button" 效果很好!感谢您的帮助
  • @CGurrell:请参阅上面 BalusC 的评论。看起来你有一辆保时捷 911,但试着把它当作家用车来使用。
【解决方案2】:

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

希望这会有所帮助!

【讨论】:

  • 好的,window.location.replace 和 window.location.href 不想工作,但是 window.open 确实有效,因为它打开了我希望它在新页面中打开的页面窗户。问题是它使我要关闭的页面保持打开状态。
  • 嗯。正如下面的评论所说,您为什么不尝试使用带有提交的表单?对我来说听起来是个好主意!
猜你喜欢
  • 1970-01-01
  • 2017-06-29
  • 2015-03-17
  • 1970-01-01
  • 1970-01-01
  • 2020-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多