阻止默认的行为

<!--阻止默认的行为-->
<form action="http://baidu.com">
用户名:<input type="text" id="username" />
<input type="submit" value="提交" id="sub"/>
</form>
<div id="msg"></div>
<script>
$(function(){
$('#sub').bind('click',function(event){
var username=$('#username').val();
if((username=="")||(username==" ") ){
$('#msg').html('<p>文本框的内容不能是空</p>');
event.preventDefault();//阻止默认的行为
}
})
})
</script>

posted @ 2017-07-20 10:41  wangmei  阅读(143)  评论(0编辑  收藏  举报