点击button自动提交表单原因及解决方案

15 篇文章 0 订阅
3 篇文章 0 订阅

在做登录的时候,需要ajax提交验证,但是发现点击button会自动submit表单,代码如下

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录</title>
<style type="text/css">
html {
  width: 100%;
  height: 100%;
}

body {
  width: 100%;
  height: 100%;
  margin: 0;
  background-color: #3C8DBC;
}

form {
  width: 400px;
  height: 400px;
  top: 50%;
  left: 50%;
  margin-left: -200px;
  margin-top: -200px;
  position: absolute;
  background-color: #FFF;
  background-color: rgba(255, 255, 255, 0.3);
  display: flex;
  border-radius: 10px;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  box-shadow: 15px 0 15px -15px #000, -15px 0 15px -15px #000; -
  -borderWidth: 2px;
  border-radius: var(- -borderWidth);
}

h1 {
  color: #FFF;
  font-weight: bold;
  text-shadow: 2px 2px 5px #002478;
}

.loginInput {
  width: 173px;
  height: 24px;
}

.loginBtn {
  height: 30px;
  color: white;
  font-size: 16px;
  background-color: #3C8DBC;
  border-color: #3C8DBC;
  border-radius: 4px;
}

.loginBtn:hover {
  background-color: #1360a7;
  border-color: #1360a7;
}
</style>
</head>
<body>
  <form action="login.do">
    <h1>系统登陆</h1>
    <hr>
    <input type="text" class="loginInput" placeholder="登录帐号"><br> <input
      type="password" class="loginInput" placeholder="登录密码"><br>
    <button class="loginInput loginBtn">按钮点击</button>
  </form>
</body>
</html>

点击提交后,会提交到form指定的action

原来是button在form里面会submit事件,最后只能改成input,然后将类型设置为submit,然后写js方法返回false即可不自动提交表单

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录</title>
<style type="text/css">
html {
  width: 100%;
  height: 100%;
}

body {
  width: 100%;
  height: 100%;
  margin: 0;
  background-color: #3C8DBC;
}

form {
  width: 400px;
  height: 400px;
  top: 50%;
  left: 50%;
  margin-left: -200px;
  margin-top: -200px;
  position: absolute;
  background-color: #FFF;
  background-color: rgba(255, 255, 255, 0.3);
  display: flex;
  border-radius: 10px;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  box-shadow: 15px 0 15px -15px #000, -15px 0 15px -15px #000; -
  -borderWidth: 2px;
  border-radius: var(- -borderWidth);
}

h1 {
  color: #FFF;
  font-weight: bold;
  text-shadow: 2px 2px 5px #002478;
}

.loginInput {
  width: 173px;
  height: 24px;
}

.loginBtn {
  height: 30px;
  color: white;
  font-size: 16px;
  background-color: #3C8DBC;
  border-color: #3C8DBC;
  border-radius: 4px;
}

.loginBtn:hover {
  background-color: #1360a7;
  border-color: #1360a7;
}
</style>
</head>
<body>
  <form action="login.do" onsubmit="return login();">
    <h1>系统登陆</h1>
    <hr>
    <input type="text" class="loginInput" placeholder="登录帐号"><br> 
    <input type="password" class="loginInput" placeholder="登录密码"><br>
    <input type="submit" class="loginInput loginBtn" value="登录"><br>
  </form>
  <script type="text/javascript">
  	function login() {
  	  // 此处写 ajax 请求后台代码
  	  alert('登录成功');
  	  return false;
  	}
  </script>
</body>
</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

svygh123

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值