jsp中的一个简单的js验证,帮忙做一下,就验证课程名不为空,非常感谢

<body><div style="padding-top:10px;height:28px; background-image:url(images/main_10.gif) ; background-repeat:repeat-x;width: 1165px;margin-left: -15px;margin-top: -16px;font-size: 12px"><img src="images/main_14.gif" style="margin-left: 20px;"/>
您现在所在的位置:管理员 → 课程维护 → <span class="bfont">添加课程</span></div><br><br>
<form action="AdminCM_addCourse.action" method="post">
<center>
<table>
<tr>
<td>课程名</td>
<td><s:textfield name="subject.name" theme="simple" size="25"></s:textfield></td>
</tr>
<tr>
<td><s:reset value="重置" theme="simple"></s:reset></td>
<td><s:submit value="提交" theme="simple"></s:submit></td>
</tr>
</table></center>
</form>
</body>
</html>

在你的onblur="check()><s:textfield name="subject.name" theme="simple" size="25"></s:textfield>里面加上这个事件..然后加一个id..
你的这个用的sturs2 我不知道能加ID不..然后再上面添加一个
<script type="text/javascript">
function test() {
var s1 = document.getElementById("你的课程名id").value;
if (s1 == "") {
alert("xxx不能为空"); //或者在你的输入框旁边加一个span来显示错误信息也行..
} }
</script>追问

还有个问题,照你的方法验证了,但不管成功还是失败都还是跳转到后台执行了后台代码

温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-06-09
你用的是struts2是吗?可以通过配置文件来设置的。
第2个回答  2011-06-09
最好在<s:textfield name="subject.name" theme="simple" size="25"> 加一个 id="name"

然后在
<script type="text/javascript">
$(function(){
$("#name").blur(function(){
var name =$(this).val();
if(name==""){
为空操作
}else{
不为空操作
}
});
});
</script>
第3个回答  2011-06-09
<script type="text/javascript">
function test() {
var s1 = document.getElementById("你的课程名id").value;
if (s1 == "") {
alert("课程不能为空不能为空");
}
return false;
}
</script>

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网