日期:2014-05-16  浏览次数:20415 次

一个Form表单提交应用.
<script   language= "javascript ">
function   btn_onClick(){
if(document.formsearch.keyword.value.length> 30)  
{  
alert( "error   ! ");  
document.formsearch.keyword.focus();  
}  
}
</script>

<form   action= "fight.php "   name= "formsearch ">
<input   name= "keyword "   type= "text "   id= "keyword ">  
<input   name= "button "   value= "button "   onClick= "btn_onClick() ">
</form>

现在的问题是:当keyword文本框中内容长度> 30时弹出error错误,但还是提交过去了.
请问大家怎么控制错误就返回,正确才能提交呢   ?

------解决方案--------------------
表单提交加入return false;
有好几种加法,例如
<script language= "javascript ">
function btn_onClick(){
if(document.formsearch.keyword.value.length> 30)
{
alert( "error ! ");
document.formsearch.keyword.focus();
return false;
}

}
</script>

<form action= "fight.php " name= "formsearch ">
<input name= "keyword " type= "text " id= "keyword ">
<input name= "button " value= "button " onClick= "return btn_onClick() ">
</form>
------解决方案--------------------
<script language= "javascript ">
function btn_onClick(){
if(document.formsearch.keyword.value.length> 30)
{
alert( "error ! ");
document.formsearch.keyword.focus();
return false;
}
}
</script>

<form action= "fight.php " name= "formsearch ">
<input name= "keyword " type= "text " id= "keyword ">
<input type= "submit " value= "button " onClick= " return btn_onClick() ">
</form>