问题如题,我在网上查有的说是脚本错误,有的说<form id="form1" name="form1" method="post" onsubmit="return checkForm()" action="*.do?method=**">onsubmit放在form标签中是先提交表单后调用方法,改成<input type="submit" name="Submit" value="添加" onclick="return checkForm()" />是先验证后提交,但是我检查了js(没有错误)也把验证的改到了onclick事件里,依然验证return false后提交数据,现在把代码贴出:
<script language="javascript" type="text/javascript">
function checkForm(){
var code = document.getElementById("berthcode");
var name = document.getElementById("berthname");
var depart = document.getElementById("parentdepart");
if(code.value.length==0){
alert("泊位代码不能为空!");
return false;
}
}
function comback(){
location.href="berthManager.do?method=getAllBerth";
}
</script>
<body>
<div align="right">带<span class="STYLE1">*</span>为必填的</div>
<form id="form1" name="form1" method="post" action="*.do?method=**">
<table width="575" border="0" align="center" cellpadding="3" cellspacing="1" class="table_style">
<tr>
<td width="25%"><span class="left-title">泊位代码:</span></td>
<td width="75%">
<input name="berthcode" type="text" id="berthcode" />
</td>
</tr>
<tr>
<td><span class="left-title">泊位名称:</span></td>
<td>
<input name="berthname" type="text" id="berthname" />
</td>
</tr>
<tr>
<td><span class="left-title">所属部门:</span></td>
<td>
<select name="parentdepart" id="parentdepart" >
<option value="0">--请选择--</option>
<logic:iterate id="de" name="alldeparts">
<option value="${de.departmentid }"><bean:write name="de" property="departmentname"/></option>
</logic:iterate>
</select>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="Submit" value="添加" onclick="return checkForm()" />
<input name="ds" type="button" id="reset" value="返回" onclick="comback()"/> </td>
</tr>
</table>
</form>希望各位大侠给予指导,先谢过.....在线等....
<script language="javascript" type="text/javascript">
function checkForm(){
var code = document.getElementById("berthcode");
var name = document.getElementById("berthname");
var depart = document.getElementById("parentdepart");
if(code.value.length==0){
alert("泊位代码不能为空!");
return false;
}
}
function comback(){
location.href="berthManager.do?method=getAllBerth";
}
</script>
<body>
<div align="right">带<span class="STYLE1">*</span>为必填的</div>
<form id="form1" name="form1" method="post" action="*.do?method=**">
<table width="575" border="0" align="center" cellpadding="3" cellspacing="1" class="table_style">
<tr>
<td width="25%"><span class="left-title">泊位代码:</span></td>
<td width="75%">
<input name="berthcode" type="text" id="berthcode" />
</td>
</tr>
<tr>
<td><span class="left-title">泊位名称:</span></td>
<td>
<input name="berthname" type="text" id="berthname" />
</td>
</tr>
<tr>
<td><span class="left-title">所属部门:</span></td>
<td>
<select name="parentdepart" id="parentdepart" >
<option value="0">--请选择--</option>
<logic:iterate id="de" name="alldeparts">
<option value="${de.departmentid }"><bean:write name="de" property="departmentname"/></option>
</logic:iterate>
</select>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="Submit" value="添加" onclick="return checkForm()" />
<input name="ds" type="button" id="reset" value="返回" onclick="comback()"/> </td>
</tr>
</table>
</form>希望各位大侠给予指导,先谢过.....在线等....
...
<html:submit value=" Save "/>
改成 <input type="button" onclick="checkForm()" value="添加">然后改下checkForm这个方法function checkForm(){
var form=document.getElementById("form1");
if(form.berthcode.value.length==0){
alert("泊位代码不能为空!");
return false;
}
form.submit();
}
这样写试试
onsubmit="return checkForm()" 这样写就对了 按纽上就不用调用函数了另外 也可以通过BUTTON来调用这个函数 然后通过函数去提交
<form id="form1" name="form1" method="post" onsubmit="return checkForm()" action="*.do?method=**">这个是checkForm()为TRUE时才提交
<input type="submit" name="Submit" value="添加" onclick="return checkForm()" />
这只是一个点击事件而已
alert("泊位代码不能为空!");
return false;
}
后面要返回一个return true;