读书人

关于用javascript 自动计算的有关问题

发布时间: 2012-02-04 15:43:09 作者: rapoo

关于用javascript 自动计算的问题
我做的一个考评表是用excel先设计好的 然后用JSP做 主要涉及到的自动计算有些麻烦,包括所有行的自动计算,最后还要把 最后一列的得分自动计算出来 而且有自动增加行的功能 就是每行的表单的name是一样的 所以计算最后一列的得分比较困难,下面是个样例,哪位高手帮忙解决一下,我想实现的就是先每行各自计算 最后把等于号后面的3个加起来该什么实现,因为他们的name相同


<html>
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
<title> +  = </title>
</head>
<script type= "text/javascript ">
function startCalc(){
interval = setInterval( "calc() ",1);
}
function calc(){
one = document.autoSumForm.firstBox.value;
two = document.autoSumForm.secondBox.value;
document.autoSumForm.thirdBox.value = (one * 1) + (two * 1);
}
function stopCalc(){
clearInterval(interval);
}
</script>
<form name= "autoSumForm ">
<input type=text name= "firstBox " value= " " onFocus= "startCalc(); " onBlur= "stopCalc(); "> +
<input type=text name= "secondBox " value= " " onFocus= "startCalc(); " onBlur= "stopCalc(); "> =
<input type=text name= "thirdBox "> <br>
<input type=text name= "firstBox " value= " " onFocus= "startCalc(); " onBlur= "stopCalc(); "> +
<input type=text name= "secondBox " value= " " onFocus= "startCalc(); " onBlur= "stopCalc(); "> =
<input type=text name= "thirdBox "> <br>
<input type=text name= "firstBox " value= " " onFocus= "startCalc(); " onBlur= "stopCalc(); "> +
<input type=text name= "secondBox " value= " " onFocus= "startCalc(); " onBlur= "stopCalc(); "> =
<input type=text name= "thirdBox ">
</form>
<p>   </p>
</body>
</html>


[解决办法]
自动记算
<html>
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
<title> +  = </title>
</head>
<script type= "text/javascript ">
function startCalc(){
interval = setInterval( "calc() ",1);
}
function calc(){

fi= document.getElementsByName( "firstBox ");
//alert(fi[0].value)
se = document.getElementsByName( "secondBox ");
thi = document.getElementsByName( "thirdBox ");
for (i=0;i <fi.length;i++){
thi[i].value=((fi[i].value)*1)+((se[i].value)*1);
}
//two = document.autoSumForm.secondBox.value;
//document.autoSumForm.thirdBox.value = (one * 1) + (two * 1);
}
function stopCalc(){
clearInterval(interval);
}
</script>
<form name= "autoSumForm ">
<input type=text name= "firstBox " value= " " onFocus= "startCalc(); " onBlur= "stopCalc(); "> +


<input type=text name= "secondBox " value= " " onFocus= "startCalc(); " onBlur= "stopCalc(); "> =
<input type=text name= "thirdBox "> <br>
<input type=text name= "firstBox " value= " " onFocus= "startCalc(); " onBlur= "stopCalc(); "> +
<input type=text name= "secondBox " value= " " onFocus= "startCalc(); " onBlur= "stopCalc(); "> =
<input type=text name= "thirdBox "> <br>
<input type=text name= "firstBox " value= " " onFocus= "startCalc(); " onBlur= "stopCalc(); "> +
<input type=text name= "secondBox " value= " " onFocus= "startCalc(); " onBlur= "stopCalc(); "> =
<input type=text name= "thirdBox ">
</form>
<p>   </p>
</body>
</html>
[解决办法]
<html>
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
<title> +  = </title>
</head>
<script type= "text/javascript ">
function startCalc(){
var ind = parseInt((event.srcElement.sourceIndex - 7)/4);
interval = setInterval( "calc( "+ind+ ") ",1);
}
function calc(i){

fi= document.getElementsByName( "firstBox ");
//alert(fi[0].value)
se = document.getElementsByName( "secondBox ");
thi = document.getElementsByName( "thirdBox ");
//for (i=0;i <fi.length;i++){
thi[i].value=((fi[i].value)*1)+((se[i].value)*1);
//}
//two = document.autoSumForm.secondBox.value;
//document.autoSumForm.thirdBox.value = (one * 1) + (two * 1);
}
function stopCalc(){
//var ind = (event.srcElement.sourceIndex - 7)%3;
clearInterval(interval);
}
function comput()
{
var result = 0;
var thirds = document.getElementsByName( "thirdBox ");
for (var i=0;i < thirds.length; i++)
{
result += parseFloat(thirds[i].value);
}
alert(result);

}
</script>
<form name= "autoSumForm ">
<input type=text name= "firstBox " value= " " onFocus= "startCalc(); " onBlur= "stopCalc(); "> +
<input type=text name= "secondBox " value= " " onFocus= "startCalc(); " onBlur= "stopCalc(); "> =
<input type=text name= "thirdBox "> <br>
<input type=text name= "firstBox " value= " " onFocus= "startCalc(); " onBlur= "stopCalc(); "> +
<input type=text name= "secondBox " value= " " onFocus= "startCalc(); " onBlur= "stopCalc(); "> =
<input type=text name= "thirdBox "> <br>
<input type=text name= "firstBox " value= " " onFocus= "startCalc(); " onBlur= "stopCalc(); "> +
<input type=text name= "secondBox " value= " " onFocus= "startCalc(); " onBlur= "stopCalc(); "> =
<input type=text name= "thirdBox ">
<input type=button value= "计算 " onclick= "comput() ">
</form>
<p>   </p>
</body>
</html>
看看是不是这样
[解决办法]
<html>


<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
<title> +  = </title>
</head>
<script type= "text/javascript ">
function startCalc(){
interval = setInterval( "calc() ",1);

}
function calc(){
//alert( "yy ");
fi= document.getElementsByName( "firstBox ");
//alert(fi[0].value)
se = document.getElementsByName( "secondBox ");
thi = document.getElementsByName( "thirdBox ");
for (i=0;i <fi.length;i++){
thi[i].value=((fi[i].value)*1)+((se[i].value)*1);
}
//two = document.autoSumForm.secondBox.value;
//document.autoSumForm.thirdBox.value = (one * 1) + (two * 1);
}
function stopCalc(){
clearInterval(interval);
}
function app(){
o=document.getElementById( "aa ");
str=o.innerHTML;
//alert(str);
o1=document.getElementById( "bb ");
//alert(o1.html);
o1.innerHTML= o1.innerHTML+ " </br> "+str;
}

function sum(){//加总
thi = document.getElementsByName( "thirdBox ");
tot = document.getElementsByName( 'totalBox ');
tot[0].value= "0 ";
for(i=0;i <thi.length;i++){
if(thi[i].value!= ' '){
tot[0].value=tot[0].value*1+thi[i].value*1;
}
}
}
</script>
<form name= "autoSumForm " id= "form1 ">
<div id= "aa "> <input type=text name= "firstBox " value= " " onFocus= "startCalc(); " onBlur= "stopCalc();sum(); "> +
<input type=text name= "secondBox " value= " " onFocus= "startCalc(); " onBlur= "stopCalc();sum(); "> =
<input type=text name= "thirdBox "> <br> </div>
<input type=text name= "firstBox " value= " " onFocus= "startCalc(); " onBlur= "stopCalc();sum(); "> +
<input type=text name= "secondBox " value= " " onFocus= "startCalc(); " onBlur= "stopCalc();sum(); "> =
<input type=text name= "thirdBox "> <br>
<div id= "bb "> <input type=text name= "firstBox " value= " " onFocus= "startCalc(); " onBlur= "stopCalc();sum(); "> +
<input type=text name= "secondBox " value= " " onFocus= "startCalc(); " onBlur= "stopCalc();sum(); "> =
<input type=text name= "thirdBox "> </div>
<input type= "button " value= "append " onclick= "app();sum() "> <span style= "margin-left:262px "> sum: <input type= "text " name= "totalBox " value= "0 "> </span>
</form>
<p>   </p>
</body>
</html>
[解决办法]
做过的一个例子,其中ds是类似于ResultSet的集合,你可以根据需要适当修改
<table id= "tb " width= "100% " border=0 cellSpacing=1 cellPadding=3 class=deep>
<tr>
<th noWrap= "true " class= "til " align= "center "> <font color= "black "> <b> 选择 </b> </font> </th>
<th noWrap= "true " class= "til " align= "center "> <b> 学号 </b> </th>
<th noWrap= "true " class= "til " align= "center "> <b> 姓名 </b> </th>


<th noWrap= "true " class= "til " align= "center "> <b> 学院 </b> </th>
<th noWrap= "true " class= "til " align= "center "> <b> 专业 </b> </th>
<th noWrap= "true " class= "til " align= "center "> <b> 类型 </b> </th>
<th noWrap= "true " class= "til " align= "center "> <b> 申报日期 </b> </th>
<%-- <th noWrap= "true " class= "til " align= "center "> <b> 院系所号 </b> </th> --%>
</tr>
<tr>
<td height=5 colspan= "15 " background= ' <%=request.getContextPath()%> /js/images/theme/toolbrbk.gif '>
<img src= " <%=request.getContextPath()%> /images/clear.gif " width= "1 " height= "1 "> </td>
</tr>
<%while(ds.next())
{
String rowid = ds.getString( "ECNUROWID ");
DataSet dds = (DataSet) lx.get(rowid);
size = dds.getRowCount()> 1?dds.getRowCount():1;
count = size+1;
%> <tr>
<td width= "32 " class= "itemnum " rowspan= " <%=size%> ">
<input type= "checkbox " id= "rowid " name= "rowid " class= "check " value= " <%=rowid %> ">
</td>
<td class= "lightcell " rowspan= " <%=size%> ">
<a href= " <%=path%> /student/EmphasesManageAction.do?method=showEmphasesManageDetail&rowid= <%=rowid%> &lsh= <%=rowid%> "> <%=ds.getString( "XH ") == null ? "&nbsp " : ds.getString( "XH ")%> </a>
</td>
<td class= "lightcell " rowspan= " <%=size%> "> <%=ds.getString( "XM ") == null ? "&nbsp " : ds.getString( "XM ")%> </td>
<td class= "lightcell " rowspan= " <%=size%> "> <%=ds.getString( "YXMC ") == null ? "&nbsp " : ds.getString( "YXMC ")%> </td>
<td class= "lightcell " rowspan= " <%=size%> "> <%=ds.getString( "ZYMC ") == null ? "&nbsp " : ds.getString( "ZYMC ")%> </td>
<%if(dds.getRowCount() > 0)
{
if(dds.next())
{%>
<td class= "lightcell " rowspan=1> <%=dds.getString( "MC ") == null ? "&nbsp " : dds.getString( "MC ")%> </td>
<% }
}
else
{
%> <td class= "lightcell " rowspan= " <%=size%> ">   </td> <%
}%>
<td class= "lightcell " rowspan= " <%=size%> "> <%=ds.getString( "SBRQ ") == null ? "&nbsp " : ds.getString( "SBRQ ")%> </td>
<td class= "lightcell " rowspan= " <%=size%> " style= "display:none; "> <%=ds.getString( "YXSH ") == null ? "&nbsp " : ds.getString( "YXSH ")%> </td>
<script language= "javascript "> yxshArray[yxshCount++]= ' <%=ds.getString( "YXSH ") == null ? "&nbsp " : ds.getString( "YXSH ")%> '; </script>
</tr> <%
while(dds.next())
{
%> <tr> <td class= "lightcell " rowspan=1> <%=dds.getString( "MC ") == null ? "&nbsp " : dds.getString( "MC ")%> </td> </tr> <%


count++;
}
} %>
</table>
[解决办法]
<param name=XMLData
value= "<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:o="urn:schemas-microsoft-com:office:office"
 xmlns:x="urn:schemas-microsoft-com:office:excel"
 xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:html="http://www.w3.org/TR/REC-html40">
<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
<Version>11.8122</Version>
</DocumentProperties>
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
<WindowHeight>9675</WindowHeight>
<WindowWidth>14940</WindowWidth>
<WindowTopX>240</WindowTopX>
<WindowTopY>105</WindowTopY>
<ProtectStructure>False</ProtectStructure>
<ProtectWindows>False</ProtectWindows>
</ExcelWorkbook>
<Styles>
<Style ss:ID="Default" ss:Name="Normal">
<Alignment ss:Vertical="Center"/>
<Borders/>
<Font ss:FontName="宋体" x:CharSet="134" ss:Size="12"/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
</Styles>
<Worksheet ss:Name="TEST">
<Table ss:ExpandedColumnCount="7" ss:ExpandedRowCount="7" x:FullColumns="1"
x:FullRows="1" ss:DefaultColumnWidth="54" ss:DefaultRowHeight="14.25">
<Column ss:Width="31.5" ss:Span="4"/>
<Column ss:Index="6" ss:Width="36"/>
<Column ss:Width="27"/>
<Row>
<Cell ss:MergeAcross="5" ><Data ss:Type="String">TEST</Data></Cell>
</Row>
<Row>
<Cell />
<Cell />
<Cell />
<Cell />
<Cell />
<Cell />
</Row>
<Row>
<Cell />
<Cell />
<Cell />
<Cell />
<Cell />
<Cell />
</Row>
<Row>
<Cell ><Data ss:Type="String">字段1</Data></Cell>
<Cell ><Data ss:Type="String">字段2</Data></Cell>
<Cell ><Data ss:Type="String">本期</Data></Cell>
<Cell ><Data ss:Type="String">累计</Data></Cell>
<Cell ><Data ss:Type="String">指标</Data></Cell>
<Cell ><Data ss:Type="String">完成度</Data></Cell>
<Cell ><Data ss:Type="String">排名</Data></Cell>
</Row>
<Row>
<Cell />
<Cell />
<Cell />
<Cell />
<Cell />
<Cell ss:Formula="=IF(RC[-1]<>0,RC[-2]/RC[-1],0)"><Data
ss:Type="Number">0</Data></Cell>
<Cell />
</Row>
<Row>
<Cell />
<Cell />
<Cell />
<Cell />
<Cell />
<Cell ss:Formula="=IF(RC[-1]<>0,RC[-2]/RC[-1],0)"><Data
ss:Type="Number">0</Data></Cell>
<Cell />
</Row>
<Row>
<Cell ><Data ss:Type="String">合计</Data></Cell>
<Cell />
<Cell ss:Formula="=SUM(R[-2]C:R[-1]C)"><Data ss:Type="Number">0</Data></Cell>
<Cell ss:Formula="=SUM(R[-2]C:R[-1]C)"><Data ss:Type="Number">0</Data></Cell>
<Cell ss:Formula="=SUM(R[-2]C:R[-1]C)"><Data ss:Type="Number">0</Data></Cell>
<Cell ss:Formula="=IF(RC[-1]<>0,RC[-2]/RC[-1],0)"><Data
ss:Type="Number">0</Data></Cell>
<Cell />
</Row>
</Table>
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<Print>
<ValidPrinterInfo/>
<PaperSizeIndex>9</PaperSizeIndex>
<HorizontalResolution>600</HorizontalResolution>
<VerticalResolution>600</VerticalResolution>
</Print>
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions>
</Worksheet>
</Workbook>
 ">

读书人网 >JavaScript

热点推荐