js验证:
<div><div id="errorDiv"></div><s:select id="type" name="type" list="" headerKey="" headerValue="请选择" listKey="masterindex" listValue="mastervalue" οnchange="typeChange(this);"> </s:select>
<s:textarea id="content" name="content" cols="50" rows="6" value="请输入内容" οnfοcus="if (value =='请输入内容'){value =''}" οnblur="contentLostFocus(this);"></s:textarea>
<a href="javascript:void(0);" οnclick="javascript:save();">提交</a></div> <script type="text/javascript">function save(){ if(check()){ //save }}
//验证function check(){ var typeObj = document.getElementById(type"); var contentObj = document.getElementById("content"); typeChange(contentObj); typeLostFocus(typeObj);
contentMsg(contentObj); typeMsg(typeObj);
var result = (result_type=="0")&& (result_content=="0"); return result;}
//类型的验证var result_Type = "";function typeChange(typeObj){ var typeId = DWRUtil.getValue("type"); if(typeId==""||typeId==0){ result_type="1"; }else{ result_type="0"; } typeMsg(typeObj);}
function typeMsg(typeObj){ if(result_type=="0"){ setMessage('',true,"",typeObj.value); }else{ setMessage('',false,"请填写问题类型",""); } typeObj.focus();}
//内容的验证var result_content = "";function contentLostFocus(contentObj){ clearElement(contentObj,"请输入内容"); if(contentObj.value==""){ result_content = "1"; }else{ result_content = "0"; } contentMsg(contentObj);}function contentMsg(contentObj){ if(result_content=="0"){ setMessage('',true,"",contentObj); }else{ setMessage('',false,"请输入内容",""); contentObj.focus(); }}
function setMessage(id,isRight,innerStr,defaultStr){ var errorDivObj=document.getElementById("errorDiv"); document.getElementById(errorDivObj.id).style.display = "block"; if(isRight){ document.getElementById(errorDivObj.id).innerHTML = ""; }else{ document.getElementById(errorDivObj.id).innerHTML = innerStr; }}function clearElement(obj,defaultStr){ if(obj.value==defaultStr){ obj.value=""; }}</script>