Javascript 当我点击按钮时,页面会奇怪地刷新
这是我的密码:Javascript 当我点击按钮时,页面会奇怪地刷新,javascript,vue.js,Javascript,Vue.js,这是我的密码: <!DOCTYPE html> <html lang="zh-CN"> <head> <title>中国工程院无线投票系统</title> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> <link rel="stylesheet" href="https://cdn.bootcss.com/b
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<title>中国工程院无线投票系统</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="css/login.css">
</head>
<body>
<div class="container" id="login">
<div class="col-md-6 col-md-offset-3">
<div class="page-header">
<h1>中国工程院无线投票系统</h1>
</div>
<form>
<div class="form-group">
<label>用户名</label>
<div class="input-group">
<div class="input-group-addon"><i class="glyphicon glyphicon-user"></i></div>
<input type="text" v-model="account.username" class="form-control" placeholder="Username" required
autofocus>
</div>
</div>
<div class="form-group">
<label>密码</label>
<div class="input-group">
<div class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></div>
<input type="password" v-model="account.password" class="form-control" placeholder="Password"
required>
</div>
</div>
<button v-on:click="validate" class="btn btn-lg btn-primary btn-block">登录</button>
</form>
</div>
</div>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/axios@0.12.0/dist/axios.min.js"></script>
<script src="js/login.js"></script>
</body>
</html>
起初,我认为这与方法“validate”中的代码有关。但是,在我删除了里面的所有代码之后,当我单击按钮时,页面仍然会刷新,这是不应该发生的。您应该将type=“button”
添加到
中
如果未在
中指定
的类型
,则默认情况下,它的行为类似于一个提交按钮,刷新页面
:
按钮将表单数据提交到服务器。如果未指定属性,或者属性动态更改为空值或无效值,则这是默认值
使用表单元素时,提交操作将刷新页面,除非指定return false;在onsubmit属性的末尾。我相信你也有同样的问题
尝试在onclick属性的末尾(引号之前)放一个分号,然后键入returnfalse;它应该可以解决您的问题。或者您可以使用vuejs的方式来解决问题,如下所示:
<button v-on:click.prevent="validate" class="btn btn-lg btn-primary btn-block">登录</button>
登录
阻止
事件修饰符阻止默认行为
这就像在事件处理程序中使用event.preventDefault()
将@submit.prevent添加到表单中一样。
....
按钮
默认类型将在单击时重新加载页面。将type='button'
添加到按钮
@mrogers谢谢<代码>返回false
不正确e.preventDefault
是正确的方法。@XINDILI如果这对你有帮助,也许你应该接受这个答案。
<button v-on:click.prevent="validate" class="btn btn-lg btn-primary btn-block">登录</button>
<form @submit.prevent>
....
</form>