利用JavaScript阻止表单提交的两种方法
作者:daisy 时间:2024-04-22 22:34:44
在JavaScript中,阻止表单默认提交行为的方法有两种,分别是:
(1) return false
示例代码
<form name="loginForm" action="login.aspx" method="post">
<button type="submit" value="Submit" id="submit">Submit</button>
</form>
<script>
var submitBtn = document.getElementById("submit");
submitBtn.onclick = function (event) {
alert("preventDefault!");
return false;
};
</script>
(2) 使用preventDefault()
在标准浏览器中,阻止浏览器默认行为使用event.preventDefault()
,而在IE6~8中,使用returnValue
属性来实现。
示例代码
<form name="loginForm" action="login.aspx" method="post">
<button type="submit" value="Submit" id="submit">Submit</button>
</form>
<script>
var submitBtn = document.getElementById("submit");
submitBtn.onclick = function (event) {
alert("preventDefault!");
var event = event || window.event;
event.preventDefault(); // 兼容标准浏览器
window.event.returnValue = false; // 兼容IE6~8
};
</script>
标签:js,阻止,表单提交
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Mysql中复制详细解析
2024-01-13 20:46:25
asp解决防止表单重复提交的方法
2007-10-19 18:40:00
教你两步解决conda安装pytorch时下载速度慢or超时的问题
2022-04-12 17:19:59
![](https://img.aspxhome.com/file/2023/5/110435_0s.png)
Golang 函数执行时间统计装饰器的一个实现详解
2024-05-09 09:46:22
Python时间模块datetime、time、calendar的使用方法
2023-04-02 06:44:16
如何使用SQL Mail收发和自动处理邮件?
2010-05-16 15:10:00
Python reques接口测试框架实现代码
2023-10-07 12:47:08
![](https://img.aspxhome.com/file/2023/9/63219_0s.png)
Django自定义YamlField实现过程解析
2021-01-14 16:43:20
详解Django中Request对象的相关用法
2021-12-08 16:30:06
![](https://img.aspxhome.com/file/2023/6/99436_0s.jpg)
Python实战之实现简易的学生选课系统
2023-10-07 02:49:37
![](https://img.aspxhome.com/file/2023/6/110206_0s.png)
一文详解Go中方法接收器的选择
2024-05-28 15:36:32
![](https://img.aspxhome.com/file/2023/9/123879_0s.png)
thinkphp学习笔记之多表查询
2023-11-15 02:57:15
实例讲解实现SQL下的字符串拆分具体方法
2008-12-09 14:55:00
ASP获取远程文件大小信息(通过header头信息)
2010-03-11 21:25:00
laravel执行php artisan migrate报错的解决方法
2024-06-05 09:44:39
![](https://img.aspxhome.com/file/2023/6/122496_0s.jpg)
30个出色的分页设计
2009-05-12 17:49:00
![](https://img.aspxhome.com/file/UploadPic/20095/12/220023a63-30s.jpg)
python opencv画局部放大图实例教程
2023-12-01 09:55:55
![](https://img.aspxhome.com/file/2023/9/93999_0s.jpg)
简单的文本内容处理工具
2010-01-28 12:31:00
Python之维度dim的定义及其理解使用方式
2021-06-10 12:33:04
![](https://img.aspxhome.com/file/2023/6/95386_0s.png)
JavaScript实现字符串与日期的互相转换及日期的格式化
2024-06-05 09:13:33