js不熟,特来求助,需自定义函数让id为input3_" & arr_linei & "_" & rownum & "的文本框value等于1和2之和<%
response.Write("<td><INPUT id=input1_" & arr_linei & "_" & rownum & " type=text  value=" & agocost & " size=3></td>")
response.Write("<td><INPUT id=input2_" & arr_linei & "_" & rownum & " type=text  value=" & sumcost & " size=3></td>")
response.Write("<td><INPUT id=input3_" & arr_linei & "_" & rownum & " type=text  value=" " size=3></td>")
%>

解决方案 »

  1.   


    <script type="text/javascript">
    function sum()
    {
      for(var j=1;j++){
      for(var i=1;i++){
      var line,row; 
      agocost="input1_"+j+"_"+i;
      sumcost="input2_"+j+"_"+i;
      totalcost="input3_"+j+"_"+i;  document.getElementById(totalcost).value==document.getElementById(agocost).value+document.getElementById(sumcost).value
      }
      }
      }
    </script>
    自己写的,但运行没效果,大家帮忙看看问题在哪里?
      

  2.   

    更正下
    <script type="text/javascript">
    function sum()
    {
      for(var j=1;j++){
      for(var i=1;i++){
      var agocost,sumcost,totalcost; 
      agocost="input1_"+j+"_"+i;
      sumcost="input2_"+j+"_"+i;
      totalcost="input3_"+j+"_"+i;  document.getElementById(totalcost).value==document.getElementById(agocost).value+document.getElementById(sumcost).value
      }
      }
      }
    </script>
      

  3.   

    var s = document.getElementById("input1_"+j+"_"+i).value;
    var b = document.getElementById("input2_"+j+"_"+i).value;
    document.getElementById("input3_"+j+"_"+i).value=s+b;
    还有你的for循环怎么结束?
      

  4.   

    i和j是asp网页中的变量参数,i和j是动态的,我也不解不知道该如何写这个函数
    接下来还会增加个文本框,value等于(input2_1_1,input2_2_1 ... input_j_1)之和
    那个自定义函数有相通之处
      

  5.   

    asp里面,arr_linei、 rownum 有范围吗,例如从1到多少之间,还是固定等于几的值?不能asp里面相加吗,如response.Write("<td><INPUT id=input3_" & arr_linei & "_" & rownum & " type=text value="&(agocost +sumcost)&" size=3></td>")
      

  6.   

    arr_linei、 rownum没有范围,是根据数据动态得到的,不能在asp中相加(里面还有循环,值就重复累加了)
    我想得到,如:input3_1_1.value=input1_1_1.value+input2_1_1.value(自定义函数中不知道该如何写)
      

  7.   

    arr_linei、 rownum怎么个动态法?
      

  8.   

    当月 多少员工(rownum)做过多少不重复个项目数(arr_linei)
      

  9.   


    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <TITLE> New Document </TITLE>
    <META NAME="Generator" CONTENT="EditPlus">
    <script type="text/javascript">
    <!--
    function $(id){
    return document.getElementById(id);
    }
    function stotal(){  
      for (i=0;i<document.form1.price.length;i++)
      {   
      if(document.form1.price.item(i).checked==true){$("total").innerHTML="¥"+document.form1.price.item(i).value*$("num").value;}   
      }   
    }
    function init(){
    stotal();
    }
    -->
    </script>
    </HEAD>
    <BODY onLoad="init()">
    <table>
    <tr>
      <td>价格</td>
      <td>数量</td>
      <td>金额</td>
    </tr>
    <form id="form1" name="form1">
    <tr>
      <td><input type="radio" id="price1" name="price" checked onClick="stotal()" value="168"> </td>
      <td>1磅 13*13(cm)</td>
      <td> 168元</td>
    </tr>
    <tr>
      <td><input type="radio" id="price1" name="price" onClick="stotal()" value="269"> </td>
      <td>2磅 17*17(cm)</td>
      <td> 269元</td>
    </tr>
    <tr>
      <td><input type="radio" id="price1" name="price" onClick="stotal()" value="439"> </td>
      <td>3磅 23*23(cm)</td>
      <td> 439元</td>
    </tr>
    <tr>
      <td><input type="radio" id="price1" name="price" onClick="stotal()" value="650"> </td>
      <td>5磅 30*30(cm)</td>
      <td> 650元</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td align="right" >购买数量:</td>
      <td><label>
      <select name="num" id="num" onChange="stotal()">
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
      <option value="4">4</option>
      <option value="5">5</option>
      <option value="6">6</option>
    <option value="7">7</option>
      <option value="8">8</option>
      </select>
      </label></td>
    </tr>
    <tr>
      <td><span style="text-align:right">总计</span></td>
      <td > <div id="total" ></div></td>
      <td>元</td>
    </tr>
    </form>
    </table>
    </BODY>
    </HTML>
    不知道  你要的是不是这样的?
      

  10.   

    谢谢楼上的,我还是把我的全部代码贴上吧,我写的函数肯定有问题,不知道该如何改
    <%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
    <!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>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    <script type="text/javascript">
    function sum(id){  for(var j=1;j++){
      for(var i=1;i++){
      var agocost,sumcost,totalcost; 
      agocost="input1_"+j+"_"+i;
      sumcost="input2_"+j+"_"+i;
      totalcost="input3_"+j+"_"+i;
      var a==document.getElementById(agocost).value;
      var s==document.getElementById(sumcost).value;  document.getElementById(totalcost).value==a+s;
      }
      }
      }
    </script>
    </head>
    <body>
    <!--#include file="conn.asp"-->
    <table border="1"   cellpadding="0" cellspacing="0">
      <tr>
        <td align="center">项目名</td>
        <td align="center">员工名</td>
         <%
         set rs=conn.execute("select a.name,b.username from userinf as a ,wl_check as b where cint(b.month)=cint(month(now)) and cint(b.year)=cint(year(now)) and b.confirm='1' and a.username=b.username ")
     arr_row=rs.GetRows
     arr_rows=Ubound(arr_row,2)  '得到数组中数据的下标
     For arr_rowi=0 To arr_rows 
         username=arr_row(0,arr_rowi)
     wl_name=arr_row(1,arr_rowi)
             response.Write("<td width='30' align='center'>"&username&"("&wl_name&")")
     next
         response.Write"</td>"
     rs.close
     response.Write"</tr>"
     
       set sql=conn.execute("select distinct a.prj_name from wl_per_project as a,wl_check as b where cint(a.month)=cint(month(now)) and cint(a.year)=cint(year(now)) and cint(b.month)=cint(month(now)) and cint(b.year)=cint(year(now)) and a.username=b.username ")
     arr_line=sql.GetRows
     arr_lines=Ubound(arr_line,2)  '得到数组中数据的下标
     For arr_linei=0 To arr_lines 
         prj_name=arr_line(0,arr_linei)
     response.Write"<tr>"
             response.Write("<td rowspan='3' width='180' align='left'>"&prj_name&"</td>")
     response.Write("<td>以往投入</td>")
      set ago_rs=conn.execute("select sum(worklog.prj_cost_time) as agocost,worklog.username  from worklog,wl_per_project where worklog.username=wl_per_project.username and worklog.prj_id=wl_per_project.prj_id  and wl_per_project.prj_name='" & prj_name & "'  and cdate(worklog.prj_date)<'2011-2-1' and worklog.username in (select username from wl_check where  confirm='1' and datediff('d',Cdate(year & '-' & month & '-' & '01'),'2011-2-1')>0) group by worklog.username ")
     arr_ago=ago_rs.GetRows
     arr_agos=Ubound(arr_ago,2)
     for arr_agoi=0 to arr_agos
         agocost=arr_ago(0,arr_agoi)
     agousername=arr_ago(1,arr_agoi)
             next
     for rownum=0 to arr_rows
     ago_name=arr_row(1,rownum)
     if agousername=ago_name then
     response.Write("<td><INPUT id=input1_" & arr_linei & "_" & rownum & " type=text  value=" & agocost & " size=3 ></td>")
             else
     response.Write("<td><INPUT type=text id=input1_" & arr_linei & "_" & rownum & " size='3'></td>")
     end if
     next
     response.Write"</tr>"

               response.Write"<tr>"
               response.Write("<td>当月投入</td>")
          for rownum=0 to arr_rows
      wl_name=arr_row(1,rownum)
          set rs=conn.execute("select sum(worklog.prj_cost_time) as sumcost  from worklog,wl_per_project where worklog.username='" & wl_name & "' and wl_per_project.username='" & wl_name & "' and worklog.prj_id=wl_per_project.prj_id and cint(wl_per_project.month)=cint(month(now)) and cint(wl_per_project.year)=cint(year(now)) and month(worklog.prj_date)=month(now) and year(worklog.prj_date)=year(now) and wl_per_project.prj_name='" & prj_name & "'")
     sumcost=rs("sumcost")
              response.Write("<td><INPUT id=input2_" & arr_linei & "_" & rownum & " type='text' value='"&sumcost&"'  size='3'></td>")
          next
                response.Write"</tr>"

               response.Write"<tr>"
               response.Write("<td>累计投入</td>")
          for rownum=0 to arr_rows
              response.Write("<td><INPUT id=input3_" & arr_linei & "_" & rownum & " type='text'  size='3' onblur='sum()'></td>")
          next
                response.Write"</tr>"      next
          %>
    </table>
    </body>
    </html>
      

  11.   

    分析代码如下,思路参考:  土办法:JS和ASP传递变量,因为所有input是ASP后台动态输出的,也就是说其个数在JS端无法预先知道循环次数,所以你可以考虑前后端传输的问题,也就是在后台通过<% response.write('var index=通过ASP获取的循环数')%>,最后记得这句要放到<script>标签里,否则JS无法引用!
     然后在前端就可以根据ASP里定义的JS变量index来获取输出了几次循环了!  好方法:不管ASP里的循环是几次,所有的INPUT ,用JQUERY的each方法来遍历,然后根据ID特征来判断如何加,这个其实我认为是比较好的方法!
      

  12.   

      <SCRIPT LANGUAGE="JavaScript">
      <!--
    var n=<%=arr_rows%>;
    var m=<%=arr_lines%>;  //-->
      </SCRIPT>
    for(var j=1;j<n;j++){
      for(var i=1;i<m;i++){
      var agocost,sumcost,totalcost; 
      agocost="input1_"+j+"_"+i;
      sumcost="input2_"+j+"_"+i;
      totalcost="input3_"+j+"_"+i;
      var a==document.getElementById(agocost).value;
      var s==document.getElementById(sumcost).value;  document.getElementById(totalcost).value==a+s;
      }
      }
      }
      

  13.   

    代码执行时这句代码报错
    var a=document.getElementById(agocost).value;
    错误:缺少对象;
      

  14.   

    谢谢各位!!
      <SCRIPT LANGUAGE="JavaScript">
      <!--
    var n=<%=rownum%>;
    var m=<%=arr_linei%>;  //-->window.onload=function sum(){  for(var j=1;j<m;j++){
      for(var i=1;i<n;i++){
      var agocost,sumcost,totalcost; 
      agocost="input1_"+j+"_"+i;
      sumcost="input2_"+j+"_"+i;
      totalcost="input3_"+j+"_"+i;
      var a=document.getElementById(agocost).value;
      var s=document.getElementById(sumcost).value;  document.getElementById(totalcost).value=a+s;  }
      }
      }
    </script>
    还有个问题,一、是a+s变成了字符串连接,(需要是数字相加)
      

  15.   

    自己修改了下,还是有点问题,当值为空时如何不显示NaN
        <SCRIPT LANGUAGE="JavaScript">
      <!--
    var n=<%=rownum%>;
    var m=<%=arr_linei%>;  //-->window.onload=function sum(){  for(var j=0;j<m;j++){
      for(var i=0;i<n;i++){
      var agocost,sumcost,totalcost; 
      agocost="input1_"+j+"_"+i;
      sumcost="input2_"+j+"_"+i;
      totalcost="input3_"+j+"_"+i;
      var a=parseFloat(document.getElementById(agocost).value);
      var s=parseFloat(document.getElementById(sumcost).value);  document.getElementById(totalcost).value=a+s;  }
      }
      }
    </script>
    运行结果如下图:
    http://hi.csdn.net/attachment/201103/3/2919155_1299134478bmCO.jpg
      

  16.   

    如果a和s 是整数 就这样 
    document.getElementById(totalcost).value=parseInt(a,10)+parseInt(s,10);//10表示10进制的
    如果不是整数那就这样
    document.getElementById(totalcost).value=parseFloat(a)+parseFloat(s);
      

  17.   

    NaN就是未知的参数类型,
    出现这个是因为document.getElementById(agocost).value等于"";再用parseFloat转换造成的.
    判断一下 
    window.onload=function sum(){
    var a,s;
      for(var j=0;j<m;j++){
      for(var i=0;i<n;i++){
      var agocost,sumcost,totalcost; 
      agocost="input1_"+j+"_"+i;
      sumcost="input2_"+j+"_"+i;
      totalcost="input3_"+j+"_"+i;
     if(document.getElementById(agocost).value!=""&&document.getElementById(sumcost).value!=""){
      a=parseFloat(document.getElementById(agocost).value);
    s=parseFloat(document.getElementById(sumcost).value);
    document.getElementById(totalcost).value=a+s;
    }
      }
      }
      }
    </script>
      

  18.   

    用了循环,但是却没有保留值,最后的结果还是会出问题,每次的agocost这些获取都会是最后一个I或J的对应ID。问题依然会有!循环后最后的是会出问题的!。
      

  19.   

    给一个以前我操作的类似例子,你参考思路修改自己的代码吧!代码不多,但是绝对精辟!整体保存为ASP文件<script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript">
    $(function(){
    $('input[id$=id3]').each(function(i,obj){//点击第I个的最后一个文本框即可统计获得的前2个和
       $(obj).click(function(){
              $(obj).val(parseInt($('input[id='+(i+1)+'id1]').val())+parseInt($('input[id='+(i+1)+'id2]').val()));
             });
     });
     
    });</script>
    <%
    str=""
    For i=1 To 15 '这里循环随便加多少都可以,i为外部循环次数,j是内部循环次数即1,2,3,固定的;
        For j=1 To 3
        str=str&"第"&i&"个INPUT循环&nbsp;&nbsp;&nbsp;<input type='text' id="&i&"id"&j&" value="&i&" /><br/>"
    next
    next
    response.write str
    %>