javascript,如果用户输入正确,则转到x子页面

javascript,如果用户输入正确,则转到x子页面,javascript,html,Javascript,Html,因此,我想在我的页面中添加一个脚本,将x输入后的用户传输到分配给该输入的子页面。请尝试以下代码: <form> Password: <input type="text" name="password" id='txtPassword'> <button id='btnSubmit'>Submit</button></form> <script> var submit = document.getElementById

因此,我想在我的页面中添加一个脚本,将x输入后的用户传输到分配给该输入的子页面。

请尝试以下代码:

<form>
Password: <input type="text" name="password" id='txtPassword'>
<button id='btnSubmit'>Submit</button></form>
<script>
    var submit = document.getElementById('btnSubmit')
    submit.onclick = function () {
        var password = document.getElementById('txtPassword').value;
        if (condition1)
        {
           goes to a subpage1
        }
        else if (condition2)
        {
          goes to a subpage1
        }
        else
        {
          alert with some message
        }
    }
</script>

密码:
提交
var submit=document.getElementById('btnSubmit')
submit.onclick=函数(){
var password=document.getElementById('txtPassword')。值;
如果(条件1)
{
转到子页面1
}
否则如果(条件2)
{
转到子页面1
}
其他的
{
用一些信息提醒你
}
}
添加按钮:

<form>
 Password: <input type="text" name="password">
 <button onclick="checkPass();">Submit</button>
</form>

密码:
提交
现在在JS中

<script>

function checkPass(){
  var password = document.getElementsByName("password")[0].value;
  if ( password != "") //or any condition
  {
     window.location.href = 'page_1_url' ; //goes to a subpage1
  }
  else if (condition2) //...
  {
    window.location.href = 'page_2_url' ; //goes to a subpage1
  }
  else
  {
    alert('....') ; //alert with some message
  }
}

函数checkPass(){
var password=document.getElementsByName(“密码”)[0]。值;
如果(密码!=“”)/或任何条件
{
window.location.href='page_1_url';//转到子页面1
}
否则,如果(条件2)/。。。
{
window.location.href='page_2_url';//转到子页面1
}
其他的
{
警报(“…”);//带有某些消息的警报
}
}

尝试阅读如何将文本框中的值转换为javascript。您的问题解决了吗?哦,是的,谢谢您的帮助,不幸的是,我似乎没有足够的声誉来放弃投票。虽然我在window.location上遇到了问题,但它对我来说确实不起作用,但我想我只需要了解一下,最终我会处理好的。那么,你在加载页面后立即阅读密码?听起来不是个好办法。哦,天哪。我的错。这应该在单击提交按钮时发生。将据此进行编辑。@Dashy非常感谢。我突然忘记了:)