做项目时,有这样的一个功能:文本框不能为空,效果如下:
过程如下:
1 、给每个元素添加ID:
比如文章描述:
<input type="text"name="content" id="content" value="<s:propertyvalue="model.content"/>"style="width:920px;height:100px;" class="bg"/>
2、在js中添加checkFrom()函数:
//判空
function checkForm(){
//校验文章标题
//获得文章标题文本框的值:
var title=document.getElementById("title").value;
if (title==null||title==''){
alert("文章标题不能为空!");
return false;
}
//校验文章描述
//获得文章标题文本框的值:
var content=document.getElementById("content").value;
if (content==null||content==''){
alert("文章描述不能为空!");
return false;
}
}
3、在提交form表单的代码后,调用此方法:
<form id="userAction_save_do"name="Form1"action="${pageContext.request.contextPath}/content_save.action"method="post" enctype="multipart/form-data" οnsubmit="returncheckForm();">