首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

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

2012-02-04 
关于用javascript 自动计算的问题我做的一个考评表是用excel先设计好的然后用JSP做主要涉及到的自动计算有

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


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

热点排行