如何在点击提交时重定向我的页面?

时间:2016-10-21 17:19:57

标签: javascript html css

我在这里的代码中看不出有什么问题。如果输入'new horizo​​n gurukul',我希望它去NHGSignin.php。在单击Next按钮时,它应该重定向我,但它没有。我不明白为什么?提前谢谢!

这是我的代码:

var schoolName = document.getElementById('schoolNameBox').value.toLowerCase();

function displaySignInError() {
  switch (schoolName) {
    case 'new horizon gurukul':
      document.getElementById("schoolName").action = 'NHGLogin.php';
      break;
    default:
      console.log('ajlsdfba');
  }
}
@import url('https://fonts.googleapis.com/css?family=Catamaran:100,200,300,400,500,600,700,800,900|Droid+Sans:400,700|Josefin+Sans:100,100i,300,300i,400,400i,600,600i,700,700i|Open+Sans:300,300i,400,400i,600,600i,700,700i,800,800i|Oxygen:300,400,700|Roboto:100,100i,300,300i,400,400i,500,500i,700,700i,900,900i|Ubuntu:300,300i,400,400i,500,500i,700,700i');

#container {
  width: 30em;
  background-color: #eee;
  height: 30em;
  border-radius: 50%;
  position: fixed; 
  top: 50%; 
  left: 50%; 
  transform: translate(-50%, -50%);
  transform: -webkit-translate(-50%, -50%);
  transform: -ms-translate(-50%, -50%);
}

#wrapper {
  width: 30rem;
  height: 30rem;
  border-radius: 50%;
}


#schoolSubmitButton {
  margin-top: 35px;
  text-align: center;
  background-color: white;
  border: 2px solid #fef;
  height: 2em;
  width: 10em;
}

#schoolName {
  margin-bottom: 40px;
  position: fixed; 
  top: 60%; 
  left: 50%; 
  transform: translate(-50%, -50%);
  transform: -webkit-translate(-50%, -50%);
  transform: -ms-translate(-50%, -50%);
  text-align: center;
}

#schoolNameBox {
  height: 2em;
  border: none;
  width: 26em;
  margin-left: 0;
  padding-left: 10px;
}

#schoolSubmitButton p {
  position: relative;
  top: 50%;
  position: relative; 
  top: 30%; 
  left: 50%; 
  transform: translate(-50%, -70%);
  transform: -webkit-translate(-50%, -70%);
  transform: -ms-translate(-50%, -70%);
}

/*
::-webkit-input-placeholder {
   padding-left: 10px;
}

:-moz-placeholder {
  padding-left: 10px;
}

:-ms-input-placeholder {
  padding-left: 10px;
}
*/

#signUpPageLink {
  text-decoration: none;
  position: relative;
  top: 5em;
}

#schoolNameDiv {
  position: fixed; 
  top: 60%; 
  left: 50%; 
  transform: translate(-50%, -50%);
  transform: -webkit-translate(-50%, -50%);
  transform: -ms-translate(-50%, -50%);
  text-align: center;
}

#main-heading {
  text-align: center;
  text-decoration: none;
  font-size: 24px;
}

#schoolAvatar {
  height: 9em;
  width: 9em;
  border-radius: 50%;
  position: fixed; 
  top: 25%; 
  left: 50%; 
  transform: translate(-50%, -50%);
  transform: -webkit-translate(-50%, -50%);
  transform: -ms-translate(-50%, -50%);
}
<!DOCTYPE html>
<html>
  <head>
    <title>NHG</title>
    <meta charset="UTF-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <link type="text/css" rel="stylesheet" href="css/normalize.css"/>
    <link type="text/css" rel="stylesheet" href="css/style.css"/>
    <link type="text/css" rel="stylesheet" href="css/resposive.css"/>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
  </head>
  <body>
    <header>
            <div id="main-head">
              <!--      REMEMBER TO STYLE THE HEADING AND SIGN UP LINK        -->
              <a href="#" id="main-heading"><h2>sKoolBook</h2></a>
            </div>
    </header>
    <section>
      <div id="container">
        <div id="wrapper">
          <img src="https://i.imgsafe.org/a40bbe047e.png" alt="avatar" id="schoolAvatar" align="middle">
          <div id="schoolNameDiv">
            <form method="POST" id="schoolName" action="#">
              <input type="text" name="schoolName" id="schoolNameBox" placeholder="Enter you School Name...">
              <br>
              <button type="submit" id="schoolSubmitButton" onclick="displaySignInError();">
                <p>Next</p>
              </button> 
              <br>
            </form>
            <br>
            <a href="signUp.php" id="signUpPageLink">Don't have an account? Sign Up.</a>
          </div>
        </div>
      </div>
    </section>
    <footer>
    </footer>
    <p id="demo"></p>
    <script src="JS/script.js"></script>
  </body>
</html>

2 个答案:

答案 0 :(得分:1)

从你的回答中我猜你正试图改变表格的动作。

当你这样做时:

var schoolName = document.getElementById('schoolNameBox').value.toLowerCase();

在函数外部,只有在呈现html时才会获得schoolName,如果你想在用户点击按钮时获取schoolName,你应该这样做:

function displaySignInError() {
  var schoolName = document.getElementById('schoolNameBox').value.toLowerCase();
  switch (schoolName) {
    case 'new horizon gurukul':
      document.getElementById("schoolName").action = 'NHGLogin.php';
      break;
    default:
      console.log('ajlsdfba');
  }
}

检查下面的更新代码段。

function displaySignInError() {
  var schoolName = document.getElementById('schoolNameBox').value.toLowerCase();
  switch (schoolName) {
    case 'new horizon gurukul':
      document.getElementById("schoolName").action = 'NHGLogin.php';
      break;
    default:
      console.log('ajlsdfba');
  }
}
@import url('https://fonts.googleapis.com/css?family=Catamaran:100,200,300,400,500,600,700,800,900|Droid+Sans:400,700|Josefin+Sans:100,100i,300,300i,400,400i,600,600i,700,700i|Open+Sans:300,300i,400,400i,600,600i,700,700i,800,800i|Oxygen:300,400,700|Roboto:100,100i,300,300i,400,400i,500,500i,700,700i,900,900i|Ubuntu:300,300i,400,400i,500,500i,700,700i');

#container {
  width: 30em;
  background-color: #eee;
  height: 30em;
  border-radius: 50%;
  position: fixed; 
  top: 50%; 
  left: 50%; 
  transform: translate(-50%, -50%);
  transform: -webkit-translate(-50%, -50%);
  transform: -ms-translate(-50%, -50%);
}

#wrapper {
  width: 30rem;
  height: 30rem;
  border-radius: 50%;
}


#schoolSubmitButton {
  margin-top: 35px;
  text-align: center;
  background-color: white;
  border: 2px solid #fef;
  height: 2em;
  width: 10em;
}

#schoolName {
  margin-bottom: 40px;
  position: fixed; 
  top: 60%; 
  left: 50%; 
  transform: translate(-50%, -50%);
  transform: -webkit-translate(-50%, -50%);
  transform: -ms-translate(-50%, -50%);
  text-align: center;
}

#schoolNameBox {
  height: 2em;
  border: none;
  width: 26em;
  margin-left: 0;
  padding-left: 10px;
}

#schoolSubmitButton p {
  position: relative;
  top: 50%;
  position: relative; 
  top: 30%; 
  left: 50%; 
  transform: translate(-50%, -70%);
  transform: -webkit-translate(-50%, -70%);
  transform: -ms-translate(-50%, -70%);
}

/*
::-webkit-input-placeholder {
   padding-left: 10px;
}

:-moz-placeholder {
  padding-left: 10px;
}

:-ms-input-placeholder {
  padding-left: 10px;
}
*/

#signUpPageLink {
  text-decoration: none;
  position: relative;
  top: 5em;
}

#schoolNameDiv {
  position: fixed; 
  top: 60%; 
  left: 50%; 
  transform: translate(-50%, -50%);
  transform: -webkit-translate(-50%, -50%);
  transform: -ms-translate(-50%, -50%);
  text-align: center;
}

#main-heading {
  text-align: center;
  text-decoration: none;
  font-size: 24px;
}

#schoolAvatar {
  height: 9em;
  width: 9em;
  border-radius: 50%;
  position: fixed; 
  top: 25%; 
  left: 50%; 
  transform: translate(-50%, -50%);
  transform: -webkit-translate(-50%, -50%);
  transform: -ms-translate(-50%, -50%);
}
<!DOCTYPE html>
<html>
  <head>
    <title>NHG</title>
    <meta charset="UTF-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <link type="text/css" rel="stylesheet" href="css/normalize.css"/>
    <link type="text/css" rel="stylesheet" href="css/style.css"/>
    <link type="text/css" rel="stylesheet" href="css/resposive.css"/>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
  </head>
  <body>
    <header>
            <div id="main-head">
              <!--      REMEMBER TO STYLE THE HEADING AND SIGN UP LINK        -->
              <a href="#" id="main-heading"><h2>sKoolBook</h2></a>
            </div>
    </header>
    <section>
      <div id="container">
        <div id="wrapper">
          <img src="https://i.imgsafe.org/a40bbe047e.png" alt="avatar" id="schoolAvatar" align="middle">
          <div id="schoolNameDiv">
            <form method="POST" id="schoolName" action="#">
              <input type="text" name="schoolName" id="schoolNameBox" placeholder="Enter you School Name...">
              <br>
              <button type="submit" id="schoolSubmitButton" onclick="displaySignInError();">
                <p>Next</p>
              </button> 
              <br>
            </form>
            <br>
            <a href="signUp.php" id="signUpPageLink">Don't have an account? Sign Up.</a>
          </div>
        </div>
      </div>
    </section>
    <footer>
    </footer>
    <p id="demo"></p>
    <script src="JS/script.js"></script>
  </body>
</html>

答案 1 :(得分:0)

而不是

document.getElementById("schoolName").action = 'NHGLogin.php';

用这个:

window.location = "NHGLogin.php";