javascript,如果用户输入正确goto x子页面

时间:2014-11-21 17:54:38

标签: javascript html

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

2 个答案:

答案 0 :(得分:1)

尝试以下代码:

<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>

答案 1 :(得分:0)

添加按钮:

<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
  }
}