前段时间时间,在写jsp时遇到了一个问题,有关form表单提交,代码如下:
<div class="container">
<form id="emailAddForm" name="emailAddForm" method="post" onsubmit="return false;">
<table cellpadding="1" cellspacing="0" class="table2" id="table2">
<tr>
<th width="20%;">
Email Subject
</th>
<td>
<input type="text" id="emailName" name="emailName" style="width: 200px; height: 25px; font-family: arial; font-size: 14px;">
</td>
</tr>
<tr>
<th>
UserInfo
</th>
<td>
<div class="userDiv">
<table class="userTab">
<tr>
<th width="10%">
No
</th>
<th width="10%">
Selection
</th>
<th width="35%">
UserName
</th>
<th width="35%">
Email
</th>
</tr>
<c:forEach items="${userList}" var="user" varStatus="status">
<tr>
<td align="center">
${status.count+rowIDStart-1}
</td>
<td align="center">
<input class="userInfo" id="userInfo" type="checkbox" name="userInfo" value="${user.userID}">
</td>
<td align="left">
${user.userName}
</td>
<td align="center">
${user.emailAddress}
</td>
</tr>
</c:forEach>
</table>
<div class="operation">
</div>
</div>
</td>
</tr>
<tr>
<th>
Report
</th>
<td>
<select class="editBox_select" name="kpisCategoriesID" id="kpisCategoriesID" onchange="javascript: selectKpisInfo();" style="width: 200px;">
<option>
-- Choose kpisCategoriesName --
</option>
<c:forEach items="${categoriesList}" var="kpisCategories" varStatus="status">
<option value="${kpisCategories.kpisCategoriesID}">
${kpisCategories.kpisCategoriesName}
</option>
</c:forEach>
</select>
<select class="editBox_select" id="kpisID" name="kpisID" style="width: 200px;">
<option>
-- Choose kpisName --
</option>
</select>
</td>
</tr>
<tr>
<th>
Attachment
</th>
<td>
<input type="checkbox" name="attachment" id="attachment" value="1" onchange="validateReport(this)">
Excel
<input type="checkbox" name="attachment" id="attachment" value="2" onchange="validateReport(this)">
pdf
</td>
</tr>
</table>
<div class="operation">
<div class="oButton">
<!-- <input type="submit" class="button" value="Add"/>-->
<button class="button" onclick="addEmailInfo()">
Add
</button>
</div>
<div class="oButton1">
<button class="button" onclick="returnList()">
Return
</button>
</div>
</div>
</form>
<div class="returnDiv">
</div>
我以前不喜欢用form表单提交,一直使用Ajax方式;但这次使用了form表单方式。在form表单中button按钮不会直接提交form表单的,想必大家都知道的。
所以我就使用了button按钮,而不是<input type="submit" value="">这种方式,当我在button中写了onclick事件点击后,总是不执行onclick函数,我就很奇怪。但是更奇怪的事情发生了,当我在服务器上访问这个页面时,onclick事件竟然起作用,(注:服务器上的浏览器是IE8,我本机是IE11),我上网找了找,但是没有找到原因,最后我在form表单中加了onsubmit="return false;",在所有的浏览器上button按钮的onclick函数生效了。想必大家都知道onsubmit(),阻止from表单提交的,但是为什么button按钮可以去提交form表单呢。可能是我写法有问题,或者是别的原因,但是我单独写了一个form表单,用button还是可以提交的。事实上,button不可以提交form表单,只有<input type="submit" value="">这种方式才可以.希望大神们指点。
本文探讨了在JSP中使用form表单时遇到的问题,即button按钮无法触发onclick事件,尤其是在不同浏览器版本间的差异表现。通过实验发现,添加onsubmit=return false;可以解决此问题。
1854

被折叠的 条评论
为什么被折叠?



