用的是正则表达式里面的替换函数。
以及用typeof(obj)=="object"来判断是否为空。
<script> function onSubmit(){ var obj=document.getElementById('headerName'); var objValue=""; if((obj!=null)&&(typeof(obj)=="object")){ objValue=obj.value; objValue=objValue.replace(/ /g,""); if(objValue==''){ alert("请输入字段名称!"); return false; } }else{ alert("请输入字段名称!"); return false; } obj=document.getElementById('showType'); objValue=""; if((obj!=null)&&(typeof(obj)=="object")){ objValue=obj.value; if(objValue==''){ alert("请选择显示方式!"); return false; } }else{ alert("请选择显示方式!"); return false; } obj=document.getElementById('isRequired'); objValue=""; if((obj!=null)&&(typeof(obj)=="object")){ objValue=obj.value; if(objValue==''){ alert("请选择是否必填!"); return false; } }else{ alert("请选择是否必填!"); return false; } if(objValue=="select"){ var obj=document.getElementById('valueOptions'); var objValue=""; if((obj!=null)&&(typeof(obj)=="object")){ objValue=obj.value; objValue=objValue.replace(/ /g,""); if(objValue==''){ alert("请输入字段下拉值!"); return false; } }else{ alert("请输入字段下拉值!"); return false; } } document.qaHeaderForm.submit(); return true; } </script>