form表单中的button与submit

本文探讨了在JSP中使用form表单时遇到的问题,即button按钮无法触发onclick事件,尤其是在不同浏览器版本间的差异表现。通过实验发现,添加onsubmit=return false;可以解决此问题。

前段时间时间,在写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">
													&nbsp;&nbsp;&nbsp;&nbsp;${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>
										--&nbsp;&nbsp;Choose kpisCategoriesName&nbsp;&nbsp;--
									</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>
										--&nbsp;&nbsp;Choose kpisName&nbsp;&nbsp;--
									</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="">这种方式才可以.希望大神们指点。

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值