<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>JK:支持民族工业,尽量少买X货</title><style>
TD,INPUT{ font-size:12; }
</style>
</head><body bgcolor=EEEEEE onload="Init();">
<form name="frm" action="about:结果页面" method=post >
<h4 align=center>JK的动态明细示例</h4><table align="center" bordercolor=cccccc border=1  width=100% >
<tr>
<td width=500>
</td>
<td align=right>
      <input type="button"  value="增加" class="bottom" onclick="addDetailProcess();">&nbsp;                       
      <input type="button"  value="删除" class="bottom" onclick="delDetailProcess();calc2();">&nbsp;                       
      <input type="button"  value="完成" class="bottom" onclick="submitProcess();">
      
</td>
</tr>
</table>
<div width=100% id=thedetailtableDIV>
<table align="center" bordercolor=cccccc border=1 width=100% >
  <tr bgcolor=eeeeee id="trDetailTitles"> 
    <td ><input type=checkbox onclick="selectallcheckbox(this)"></td>
    <td >明细</td>
  </tr>
  <tbody id="tbDetailUsed" ><tr height=0 ><td colspan=100 height=0 > 合计<input name=resultTotal></td></tr>
  </tbody> </table>
</div>
</form><table id="tbDetailPrepare" style="display:none">
<tr>
<td ><input type="checkbox" name="record_select" ></td>
<td >
<select name="pid" onChange="changelist(this);">
</select>
<select name="list" onChange="changelist(this);">                  
</select>
<input name="pprice" type="text" id="pprice" size="12" onpropertychange="calc(this);" >*                   
<input name="q" type="text" id="q" size="8" onpropertychange="calc(this);" >=                   
<input name="result" type="text" id="result" onpropertychange=calc2();>

</td></tr>
</table>
</body></html><script language=javascript>
function selectallcheckbox(obj) //全选或全不选
{
var tureorfalse=obj.checked;
var theDetail=tbDetailUsed.rows;
for(var i=0;i<theDetail.length-1;i++)
{
theDetail[i].all("record_select").checked=tureorfalse;
}
}


function addDetailProcess(afterRowIndex) //增加明细
{
var alltbDetailUsed= document.all("tbDetailUsed").rows;
var theFirstSelectedDetail;
if (afterRowIndex==null)
{
for(var i=0;i<alltbDetailUsed.length-1;i++)
{
if (alltbDetailUsed[i].all("record_select").checked==true) {theFirstSelectedDetail=i;break;}
}
if (theFirstSelectedDetail==null) {theFirstSelectedDetail=alltbDetailUsed.length-2;}
}
else theFirstSelectedDetail=afterRowIndex;
var newRow = document.all("tbDetailPrepare").rows[0].cloneNode(true);
var desRow = alltbDetailUsed[theFirstSelectedDetail+1];
desRow.parentElement.insertBefore(newRow,desRow ); }

function delDetailProcess() //删除明细
{
var alltbDetailUsed= document.all("tbDetailUsed").rows;
if (confirm("确定选择正确并且要将这些明细删除")==false) return false;
for(var i=0;i<alltbDetailUsed.length-1;i++)
{
if (alltbDetailUsed[i].all("record_select").checked==true)
{
document.all("tbDetailUsed").deleteRow(i);
i=i-1;
}
}
} function submitProcess()
{
  var detailIdObjs=document.frm.getElementsByName("pid");
  for(var i=0;i<detailIdObjs.length;i++)
  {
    if(detailIdObjs[i].value=="")
    {
      alert("Please input!");
      detailIdObjs[i].focus();
      return false;
      
    }
  }

  document.frm.submit();
}
</script>
<script language="JavaScript">
function Info(pid,pname,pprice){
this.pid=pid
this.pname=pname
this.pprice=pprice
}
//以下数据由数据库取得在服务器端产生
var PInfo =new Array(6)//这个3根据记录集的数目定
//下面是用循环来产生 
PInfo[0]=new Info("001","墨盒","360.00")
PInfo[1]=new Info("002","打印纸","60.00")
PInfo[2]=new Info("003","打印机","2200.00")
PInfo[3]=new Info("004","计算机","8400.00")
PInfo[4]=new Info("005","碎纸机","720.00")
PInfo[5]=new Info("A006","碎纸机8","7890.00")
//数据结束
function Init(){
for (var i=0;i<PInfo.length;i++){
    tbDetailPrepare.all.pid.options[i]=new Option(PInfo[i].pid)
    tbDetailPrepare.all.list.options[i]=new Option(PInfo[i].pname)
    tbDetailPrepare.all.pprice.value=PInfo[i].pprice
  }
}
function calc(obj){
var currentTr=obj.parentElement.parentElement;
currentTr.all.result.value=currentTr.all.pprice.value*currentTr.all.q.value ;
}
function changelist(obj){
var currentTr=obj.parentElement.parentElement;
var x=obj.selectedIndex;
currentTr.all.pid.options[x].selected=true;
currentTr.all.list.options[x].selected=true;
currentTr.all.pprice.value=PInfo[x].pprice
}function calc2(){ var theResults=document.getElementsByName("result");
var totalTemp=0;
for(var i=0;i<theResults.length-1;i++)
{
  totalTemp=totalTemp*1+theResults[i].value*1;
}
document.frm.all.resultTotal.value=totalTemp;

}</script>

解决方案 »

  1.   

    提交的时候有错误。我直接提交到处理页面,只有最后一行数据的值。
    是不是下面的涵数有错?
    function submitProcess()
    {
      var detailIdObjs=document.frm.getElementsByName("pid");
      for(var i=0;i<detailIdObjs.length;i++)
      {
        if(detailIdObjs[i].value=="")
        {
          alert("Please input!");
          detailIdObjs[i].focus();
          return false;
          
        }
      }

      document.frm.submit();
    }
      

  2.   

    function submitProcess()
    {
      document.frm.submit();
    }
    submitProcess里的内容是判断必须输入的,可以去掉
      

  3.   

    我只是用PHP打印出传过来的值:<?PHP print_r($_POST);?>错不了的。
    如果增加明细的时候下面这些代码的name属性不动态改变的话,那前面的数据岂不都被覆盖掉了?
    <table id="tbDetailPrepare" style="display:none">
    <tr>
    <td ><input type="checkbox" name="record_select" ></td>
    <td >
    <select name="pid" onChange="changelist(this);">
    </select>
    <select name="list" onChange="changelist(this);">                  
    </select>
    <input name="pprice" type="text" id="pprice" size="12" onpropertychange="calc(this);" >*                   
    <input name="q" type="text" id="q" size="8" onpropertychange="calc(this);" >=                   
    <input name="result" type="text" id="result" onpropertychange=calc2();>

    </td></tr>
    </table>
      

  4.   

    JK_10000(JK)兄,能否再修改一下,谢谢
      

  5.   

    to :duoduobaba(避雷针) 
    想过这样改,不过一改就好多地方出错,昨回事?
      

  6.   

    <HTML>
    <HEAD>
    </HEAD>
    <BODY onLoad="Init(3)">
    <div id="bmm">
    </div>
    <input type="button" value="增加一行" onClick="document.all.bmm.innerHTML+=theRow;Init1(document.all.pid.length-1);hm++">
    总计:<input name="summed" type="text" id="summed" readonly="true">
    <script language="JavaScript" type="text/JavaScript">
    function Info(pid,pname,pprice){
    this.pid=pid
    this.pname=pname
    this.pprice=pprice
    }
    //以下数据由数据库取得在服务器端产生
    var theRow="<div><select name='pid' onChange='changelist(this.selectedIndex)'></select><select name='list' onChange='changelist(this.selectedIndex)'>"
    theRow+="</select><input name='pprice' type='text' id='pprice' size='12' onpropertychange='calc()' readonly='true'>* "
    theRow+="<input name='q' type='text' id='q' size='8' onpropertychange='calc()' onkeypress='var kc=window.event.keyCode;if(kc>32 && (kc>57 || kc<48)){return false;}'>= <input name='result' type='text' id='result' onpropertychange='summed()'></div>"
    var hm
    var PInfo =new Array(6)//这个3根据记录集的数目定
    //下面是用循环来产生 
    PInfo[0]=new Info("001","墨盒","360.00")
    PInfo[1]=new Info("002","打印纸","60.00")
    PInfo[2]=new Info("003","打印机","2200.00")
    PInfo[3]=new Info("004","计算机","8400.00")
    PInfo[4]=new Info("005","碎纸机","720.00")
    PInfo[5]=new Info("A006","碎纸机8","7890.00")
    //数据结束
    function Init(idx){
    hm=idx
    for (var i=0;i<idx;i++){
    document.all.bmm.innerHTML+=theRow;
    }
    Init1(-1)
    }
    function Init1(idx){
    if(idx<0){
    for (var j=0;j<document.all.pid.length;j++){
    for (var i=0;i<PInfo.length;i++){
    document.all.pid[j].options[i]=new Option(PInfo[i].pid);
    document.all.list[j].options[i]=new Option(PInfo[i].pname);
    document.all.pprice[j].value=PInfo[i].pprice;
    }
    }
    }else{
    for (var i=0;i<PInfo.length;i++){
    document.all.pid[idx].options[i]=new Option(PInfo[i].pid);
    document.all.list[idx].options[i]=new Option(PInfo[i].pname);
    document.all.pprice[idx].value=PInfo[i].pprice;
    }
    }
    }
    function calc(){
    var obj=event.srcElement.parentElement
    if (obj.children[2].value.length>0 && obj.children[3].value.length>0){
    obj.children[4].value=obj.children[2].value*obj.children[3].value;
    }else{
    obj.children[4].value=0;
    }
    }
    function changelist(x){
    var obj=event.srcElement.parentElement
    obj.children[0].options[x].selected=true;
    obj.children[1].options[x].selected=true;
    obj.children[2].value=PInfo[x].pprice;
    }
    function summed(){
    if (hm==1){
    document.all.summed.value=document.all.result.value;
    }else{
    var summed=0;
    for(var i=0;i<hm;i++){
    if (document.all.result[i].value.length>0){summed+=parseFloat(document.all.result[i].value);}
    }
    document.all.summed.value=summed;
    }
    }
    </script>
    </BODY>
    </HTML>
      

  7.   

    写完了才看了JK_10000(JK)的代码,汗!
      

  8.   

    回复人: caitang8(caitang) ( ) 信誉:97  2004-10-26 20:10:00  得分: 0  ----------------
    有这回事吗?
    不懂php.所以用以下方式做了下测试:
    1.将form的method改成get,将其action改成aaa.htm
    2.完成按钮的onclick改成document.frm.submit();
    3.运行页面,添加三笔记录,点击提交
    4.查看接收页的url,如下:
    res://C:\WINNT\System32\shdoclc.dll/syntax.htm#unsaved:///aaa.htm?
    pid=001&list=%C4%AB%BA%D0&pprice=7890.00&q=&result=0
    &pid=001&list=%C4%AB%BA%D0&pprice=7890.00&q=&result=0
    &pid=001&list=%C4%AB%BA%D0&pprice=7890.00&q=&result=0
    &resultTotal=0数据都已传出,没见异常啊??!!
      

  9.   

    回复人: caitang8(caitang) ( ) 信誉:97  2004-10-26 23:59:00  得分: 0  
      
       to :duoduobaba(避雷针) 
    想过这样改,不过一改就好多地方出错,昨回事?
     
    --------------------------------------
    原来你只是“想过”,但一直都没“相同ID的text做数组处理”??????
      

  10.   

    回复人: duoduobaba(避雷针) ( ) 信誉:100  2004-10-27 01:52:00  得分: 0  ----------
    惭愧啊,俺的代码过长。