首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用Javascript转到链接?

如何使用Javascript转到链接?
EN

Stack Overflow用户
提问于 2018-06-09 00:42:22
回答 1查看 333关注 0票数 0

您好,我有这段代码使用javascript & html:

代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Signup Form</title>

  <!-- css -->
  <link rel="stylesheet" href="style.css">
  <style>
    .box {
      max-width: 500px;
      margin-left: auto;
      margin-right: auto;
    }
  </style>

  <!-- js -->
  <script src="https://unpkg.com/vue"></script>
</head>
<body>

  <div class="hero is-fullheight is-info is-bold">
  <div class="hero-body">
  <div class="container">
    <h1 class="title has-text-centered">Test</h1>
    <div class="box">

      <!-- our signup form ===================== -->
      <form id="signup-form" @submit.prevent="processForm">
        <!-- name -->
        <div class="field">
          <label class="label">Username</label>
          <input 
            type="text"
            class="input" 
            name="name"
            v-model="name">
        </div>

        <!-- email -->
        <div class="field">
          <label class="label">Password</label>
          <input 
            type="password" 
            class="input" 
            name="email" 
            v-model="email"
            @blur="validateEmail">

          <p class="help is-danger" v-if="errors.email">
            Please enter a valid email.
          </p>
        </div>

        <!-- submit button -->
        <div class="field has-text-right">
          <button type="submit" class="button is-danger">
            Log In           
          </button>
        </div>
      </form>

    </div>
  </div>
  </div>
  </div>


  <script>

    var app = new Vue({
      el: '#signup-form',
      data: {
        name: '',
        email: '',
        errors: {
          name: false,
          email: false
        }
      },
      methods: {
        processForm: function() {
          console.log({ name: this.name, email: this.email });
          onclick="test.html"; 

        },
        validateEmail: function() {
          const isValid = window.isValidEmail(this.email);
          this.errors.email = !isValid;
        }
      }
    });

    function isValidEmail(email) {
      var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
      return re.test(email);
    }
  </script>
</body>
</html>

我希望当我点击按钮时,我可以通过test.html转到页面,我试图使用href = "test.html,但它不起作用。我想我必须用Javascrit编写它,但即使使用action = "test.html",它也不能工作……

你能帮我个忙吗?

非常感谢!

EN

回答 1

Stack Overflow用户

发布于 2018-06-09 06:53:21

看起来你想要做的是重定向到一个新的页面。尝试:

代码语言:javascript
复制
window.location = "test.html";

而不是:

代码语言:javascript
复制
onclick="test.html";

据我所知,onclick="test.html“所做的就是创建一个字符串值为"test.html”的变量"onclick“。将window.location设置为您要重定向到的URL将使用户导航到该URL。有关window.location的更多信息,请点击此处:https://developer.mozilla.org/en-US/docs/Web/API/Window/location

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50765100

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档