我在jsp中有表单。我有一个javascript函数,当它们都满了时动态添加字段。我的表单是100%工作,但我注意到我把元标记放在了头部之外。当我把它放回头部时,功能不再起作用了。为什么呢?
编辑:错误是“var rowCount = table.rows.length;”在javascript中。当元标记在头部之外时,rowCount正在工作,但当它在内部时,rowCount从0开始。
javascript函数:
function addRespDropDownAjax( currentNumberOfDropDown ) {
// Prepare a new ajaxRequest.
if( window.XMLHttpRequest ) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
// Ajax receiving the response in this function
xmlhttp.onreadystatechange = function() {
// state 4 is response ready.
// Status 200 is page found.
if( xmlhttp.readyState == 4 && xmlhttp.status == 200) {
//Change the content of the select to ajax result.
var content = [];
content = decode(xmlhttp.responseText);
var table = document.getElementById('tableDropDownResp');
var rowCount = table.rows.length;
alert(rowCount);
var row = table.insertRow(rowCount);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
var cell4 = row.insertCell(3);
cell1.innerHTML = content[0];
cell2.innerHTML = content[1];
cell3.innerHTML = content[2];
cell4.innerHTML = content[3];
updateLastTaskCodeSelect();
}
};
// Send the Ajax request.
xmlhttp.open(
'GET',
'mainServlet?command=ajax.AddRespDropDown&NUMBER_OF_DROP_DOWN=' +
currentNumberOfDropDown,
true );
xmlhttp.send();
}
jsp表单:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<head>
<link href="css/edit.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/form.js"></script>
<script type="text/javascript" src="js/popupCalendar.js"></script>
<title>IMG - Oracle Access Request</title>
</head>
<body>
<jsp:include page="../menuBar.jsp" />
<div class="body">
<jsp:include page="Formulaire/table1.jsp" >
<jsp:param name="isCreation" value="0" />
</jsp:include>
<jsp:include page="Formulaire/table2.jsp" />
<jsp:include page="Formulaire/table3.jsp" />
<a class="submitButton" id="submit" onclick="sendRequest('MODIFICATION')">Send Request</a>
</div>
</body>