阻止默認的行爲

<!--阻止默認的行爲-->
<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>html

相關文章
相關標籤/搜索