点击一个按钮触发onclick事件,然后执行一段js脚本,脚本执行完之后页面的表单居然提交了,脚本里面没有表单提交的代码,按钮也不是submit按钮,而且无论点击页面哪个按钮表单都会提交,代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<link href="css/tongxunlu.css" rel="stylesheet" type="text/css" />
<link href="css/tongxunlu_button.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/DD_belatedPNG_0.0.8a.js"></script>
<!--[if IE 6]>
<script src="js/DD_belatedPNG_0.0.8a.js"></script>
<script>
DD_belatedPNG.fix('img');
</script>
<![endif]-->
<script type="text/javascript">
function doSearch()
{
window.showModalDialog("doSearch.jsp",window,"resizable:no;scrollbars:auto;status:no;help:no;dialogWidth:440px;dialogHeight:300px");
}
function doImport() {
window.open("doImport.jsp");
}
function doExport() {
window.open("doExport.jsp");
}
function forward(){
document.getElementById("searchStr").value=ss;
var form=document.getElementById("searchForm");
form.action="gContactSearch.bester";
form.submit();
}
function closeIe(){
window.parent.close();
}
</script>
</head><body>
<s:form name="searchForm" method="post" theme="simple" id="searchForm" target="tongxunlu_right">
<div class="header">
<div class="header2">
<div class="header3">
<div class="titlebutoon">
<s:hidden id="searchStr" name="searchStr"></s:hidden>
<button class="dr" onclick="doImport()"><img src="_images/dr.png" width="16" height="16" style="*margin-left:5px;"/>导入通讯簿</button>
<button class="dc" onclick="doExport()"><img src="_images/dc.png" width="16" height="16" style="*margin-left:5px;"/>导出通讯簿</button>
<button class="gj" onclick="doSearch()"><img src="_images/search.png" width="16" height="16" style="*margin-left:5px;"/>高级查询</button>
</div>
</div>
</div>
</div>
</s:form>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<link href="css/tongxunlu.css" rel="stylesheet" type="text/css" />
<link href="css/tongxunlu_button.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/DD_belatedPNG_0.0.8a.js"></script>
<!--[if IE 6]>
<script src="js/DD_belatedPNG_0.0.8a.js"></script>
<script>
DD_belatedPNG.fix('img');
</script>
<![endif]-->
<script type="text/javascript">
function doSearch()
{
window.showModalDialog("doSearch.jsp",window,"resizable:no;scrollbars:auto;status:no;help:no;dialogWidth:440px;dialogHeight:300px");
}
function doImport() {
window.open("doImport.jsp");
}
function doExport() {
window.open("doExport.jsp");
}
function forward(){
document.getElementById("searchStr").value=ss;
var form=document.getElementById("searchForm");
form.action="gContactSearch.bester";
form.submit();
}
function closeIe(){
window.parent.close();
}
</script>
</head><body>
<s:form name="searchForm" method="post" theme="simple" id="searchForm" target="tongxunlu_right">
<div class="header">
<div class="header2">
<div class="header3">
<div class="titlebutoon">
<s:hidden id="searchStr" name="searchStr"></s:hidden>
<button class="dr" onclick="doImport()"><img src="_images/dr.png" width="16" height="16" style="*margin-left:5px;"/>导入通讯簿</button>
<button class="dc" onclick="doExport()"><img src="_images/dc.png" width="16" height="16" style="*margin-left:5px;"/>导出通讯簿</button>
<button class="gj" onclick="doSearch()"><img src="_images/search.png" width="16" height="16" style="*margin-left:5px;"/>高级查询</button>
</div>
</div>
</div>
</div>
</s:form>
</body>
</html>
document.getElementById("searchStr").value=ss;
var form=document.getElementById("searchForm");
form.action="gContactSearch.bester";
form.submit();把这行注释掉应该就不提交了
}
function doImport(e) {
e.preventDefault(); //阻止默认的表单提交行为
window.open("doImport.jsp");
}
请始终为按钮规定 type 属性。Internet Explorer 的默认类型是 "button",而其他浏览器中(包括 W3C 规范)的默认值是 "submit"。