<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8" /> <title>阻止默认行为</title> <script src="http://apps.bdimg.com/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script> <script type="text/javascript"> $(function(){ var $btn = $("#btn"); $btn.bind(‘click‘,function(e){ var username = $("#username").val();//获取元素的值 if(username == ‘‘){//判断是否为空 $("#show").html(‘<p>用户名不能为空</p>‘);//提示信息 e.preventDefault();//阻止默认行为(表单提交) } }) }) </script> </head> <body> <form action="test.html"> 用户名:<input type="text" id="username" /> <input type="submit" value="提交" id="btn" /> </form> <div id="show"></div> </body> </html>
时间: 2024-10-03 22:54:04