JQuery阻止表单提交的方法总结 - 使用onsubmit()验证表单并阻止非法提交
方法1:<form onsubmit="javascript:confirm()"> 方法内返回false阻止表单提交
示例:代码检测textarea内填写的长度,未填写时提示需要重新填写,少于15字符时提示需要长于15字符,成功时显示所填写建议。
1 <script type="text/javascript">
//jQuery代码
function confirm()
{
if($("#advice").val().length == 0)
{
alert("We can't see your advice. Maybe you should fill the form first.");
$("#advice").focus();
return false;
}
else if($("#advice").val().length <= 15)
{
alert("Your advice should longer than 15 letters.");
$("#advice").focus();
return false;
}
else
{
alert("Thank you for your advice! You will get out reply in 24 hours for advice:\n"+$("#advice").val());
return true;
} }
</script> <form action="" method="post" onSubmit="return confirm();" >
<textarea id="advice" rows="20" cols="50"placeholder="Give us some advice?"></textarea>
28 <input type="submit"value="Thank you"/>
</form>
>关键点
1.必须要有onSubmit="return confirm();" return 这个词,不可缺少。
2.自行完整的网页结构。
方法二:<form onsubmit="javascript:checkContent(this, event)"> 方法内返回false阻止表单提交
evt.preventDefault(); 使用JQuery event对象的方法preventDefault()阻止默认行为
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAwQAAAEICAIAAABuxKTnAAAWS0lEQVR4nO3dMXLbON8HYN7mLT3J5CpfqXHhQ3wX2Jl8KVLtLVJ43KR9T7DNznibXMLV1v4K2RJEAH+AkmNTxPMUWokCQRCEwZ8hOTs9XewZAGDF/huahCEAYNuEIQBgaMIQADA0YQgAGJowBAAMTRgCAIa2rjD0AwDgYsIQADC0Kw5DAADvTBgCAIYmDAEAQxOGAIChCUMAwNCEIQBgaMIQADA0YQgAGJowBAAMTRgCAIYmDAEAQxOGAIChCUMAwNCEIQBgaMIQADA0YQgAGJowBAAMTRgCAIYmDAEAQxOGAIChCUMAwNCEIQBgaMIQADA0YQgAGNq6w9DDbpqm6ebbY1/J3UNY5vHbzdSo77VIYH+Qh11ez37nRiMAgHVZdxhqpKFjAnp82N0cC75EmpdYsq/jJAblmedkz0OeedilR99XtD/e48O3m3nLhCEAuE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4OIvaXaH42HPLKTYp6J9aA5a2Nm9e8XZyKFbr2Fkja0ePW9Wv2MhanT0Hev+prDbsm0M3OOvaUO8ZgW8y6oL21JrUvGQ9+77VuOIdbDMMMaxgSMTz+1M2hcXTYiDdPa6zdu8M2rborWKx4KDNSpplmrel2v0saGTQV0vvOrVib3LTanZs0JjVCq5U3mm1kFHLEP2jaOn24qgrNibfq/+nMng37h9WSBhiU+IvUD+FN+PanXjRFBZPtfmcOJX0l4nVTj9vW+0UagUuuS3Vntf6P9irf8tTqROarerXc716GvnheppdvFLplnyXnmP1H7dZf2fJeDTmTQoaPCuT1tnsH9ZAGGJTDAkAlhKG2BRDAoClhCE2xZAAYClhiE0xJABYShhiUwwJAJYShtgUQwKApYQhNsWQAGCpqw9DAAAXuuIwBADwzoQhAGBowhAAMDRhCAAYmjAEAAxNGAIAhiYMAQBDE4YAgKEJQwDA0IQhAGBowhAAMDRhCAAYmjAEAAxNGAIAhiYMAQBDE4YAgKEJQwDA0IQhAGBowhAAMDRhCAAY2rrC0A8AgIsJQwDA0K44DAEAvDNhCAAYmjAEAAxNGAIAhiYMAQBDE4YAgKEJQwDA0IQhAGBowhAAMDRhCAAYmjAEAAxNGAIAhiYMAQBDi5PM/wOqqPhpmNKfkwAAAABJRU5ErkJggg==" alt="" />
参考W3School教程
示例:
<script type="text/javascript">
//jQuery代码
function checkContent(form, evt)
{
if($("#advice").val().length == 0)
{
alert("We can't see your advice. Maybe you should fill the form first.");
$("#advice").focus();
evt.preventDefault();
}else if($("#advice").val().length <= 15)
{
alert("Your advice should longer than 15 letters.");
$("#advice").focus();
evt.preventDefault();
} else {
alert("Thank you for your advice! You will get out reply in 24 hours for advice:\n"+$("#advice").val());
}
} </script> <form action="" method="post" onSubmit="javascript:checkContent(this, event)" >
<textarea id="advice" rows="20" cols="50" placeholder="Give us some advice?"></textarea>
<input type="submit" value="Thank you"/>
</form>
JQuery阻止表单提交的方法总结 - 使用onsubmit()验证表单并阻止非法提交的更多相关文章
- 利用jquery.form.js实现将form提交转为ajax方式提交的方法(带上传的表单提交)
提供一种方法就是利用jquery.form.js. (1)这个框架集合form提交.验证.上传的功能. 核心方法 -- ajaxForm() 和 ajaxSubmit() $('#myForm').a ...
- Jquery ajax提交表单几种方法
在jquery中ajax提交表单有post与get方式,在使用get方式时我们可以直接使用ajax 序列化表单$('#表单ID').serialize();就行了,下面我来介绍两个提交表单数据的方法. ...
- Jquery ajax提交表单几种方法详解
[导读] 在jquery中ajax提交表单有post与get方式,在使用get方式时我们可以直接使用ajax 序列化表单$( 表单ID) serialize();就行了,下面我来介绍两个提交表单数据的 ...
- jquery submit()不能提交表单的解决方法
<form id="form" method="get"> <input type="text" name="q ...
- jsp——js事件修改属性样式的两种方法(直接赋值、修改属性)、验证表单符合某要求、阻止表单提交、告诉浏览器不要缓存
代码 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncodi ...
- 第一百八十六节,jQuery,验证表单插件,Ajax 表单插件,验证和提交表单
jQuery,验证表单插件,Ajax 表单插件,验证和提交表单 HTML <form id="reg" method="post" action=&quo ...
- 防止Web表单重复提交的方法总结
在Web开发中,对于处理表单重复提交是经常要面对的事情.那么,存在哪些场景会导致表单重复提交呢?表单重复提交会带来什么问题?有哪些方法可以避免表单重复提交? 表单重复提交的场景 1.场景一:服务端未能 ...
- jquery.validate.js 验证表单时,在IE当中未验证就直接提交的原因
jquery.validate.js 验证表单时,在IE当中未验证就直接提交的原因 今天利用了jquery.validate.js来验证表单,发现在火狐.谷歌浏览器当中都可以进行验证,但是在IE系列浏 ...
- easy ui 表单ajax和from两种提交数据方法
说明: ①ajax在表单提交时需要将所有表单的控件的数据一一获取并赋值传到后台 ②form在提交时,只要给控件加name属性,在提交时就可以将表单数据提交到后台,不需要一一获取再进行赋值. ajax ...
随机推荐
- forward 和redirect的区别
1.从地址栏显示来说 forward是服务器请求资源,服务器直接访问目标地址的URL,把那个URL的响应内容读取过来,然后把这些内容再发给浏览器.浏览器根本不知道服务器发送的内容从哪里来的,所以它的地 ...
- Hadoop基本原理之一:MapReduce
1.为什么需要Hadoop 目前,一块硬盘容量约为1TB,读取速度约为100M/S,因此完成一块硬盘的读取需时约2.5小时(写入时间更长).若把数据放在同一硬盘上,且全部数据均需要同一个程序进行处理, ...
- jsp 、js和css
css的一些样式 1.自动换行 .AutoNewline { Word-break: break-all;/*必须*/ width :50px;/*这里是设置多宽就进行换行 */ }
- smarty模板引擎中section循环loop与total的区别
在smarty模板引擎的section循环中 $data=[101,102,103,105,104]; section的两个属性total与loop {section foo $data start= ...
- mysql中自动更新时间CURRENT_TIMESTAMP
timestamp的两个属性:CURRENT_TIMESTAMP 和ON UPDATE CURRENT_TIMESTAMP http://blog.163.com/qiongling007@126/b ...
- No enclosing instance of type test8 is accessible. Must qualify the allocation with an enclosing instance of type test8 (e.g. x.new A() where x is an
在编译一个例子时,结果编译时出现: No enclosing instance of type test8 is accessible. Must qualify the allocation wit ...
- JVM性能调优博客
http://houjixin.blog.163.com/blog/static/35628410201411275719843/ http://blog.csdn.net/lastsweetop/a ...
- 使用Discuz!自带参数防御CC攻击以及原理,修改Discuz X 开启防CC攻击后,不影响搜索引擎收录的方法
这部份的工作,以前花的时间太少. 希望能产生一定的作用. http://www.nigesb.com/discuz-cc-attacker-defence.html http://bbs.zb7.co ...
- Delphi 实现任务栏多窗口图标显示(使用WS_EX_APPWINDOW风格)
uses Windows;type TfrmLogin = class(TForm) end; implementation {$R *.dfm} procedure TfrmLogin.FormCr ...
- Android导航菜单横向左右滑动并和下方的控件实现联动
这个是美团网个人订单的效果,找了很多地方都没找到,自己研究了两天终于弄出来了^_^,有什么问题希望大家指出来,谢谢. 实现原理是上方使用HorizontalScrollView这个可以水平横向拖动的控 ...