设置一个带ID的表格,用一个按钮来动态增加行,增加的行中设置文本框,文本框设置验证条件,再通过JS验证代码在提交时验证就好了.
增加行的按钮:<input name="button"   type="button" accesskey="A" onClick="addline()" value="增加一行(A)" />
增加行的代码和删除行的代码:(验证的代码自己去找个吧,我就不贴上来了,测试时需要做增加一个ID为CaiGou的表格)<script> 
var c=0;
function   addline(content){   
  ++c;
newRow=document.all.CaiGou.insertRow(-1) newcell=newRow.insertCell()
newcell.style.backgroundColor='#f7f7f7'
newcell.innerHTML=c newcell=newRow.insertCell()  
newcell.style.backgroundColor='#FFFFFF'
newcell.innerHTML='<input name="工单号" type="text" id="工单号" size="10" maxlength="50" class="btn1" onmouseover="this.focus();" onblur="this.className=\'btn1\'" onfocus="this.className=\'input1-bor\'" onClick="OpenChild(this);" readonly="true" dataType="Require"  msg="工单号没有选择" />' newcell=newRow.insertCell()
newcell.style.backgroundColor='#FFFFFF'
newcell.innerHTML='<input name="产品名称" type="text" id="产品名称" size="15" maxlength="100" class="btn1" onmouseover="this.focus();" onblur="this.className=\'btn1\'" onfocus="this.className=\'input1-bor\'" dataType="Require"  msg="产品名称没有填写" />' newcell=newRow.insertCell()
newcell.style.backgroundColor='#FFFFFF'
newcell.innerHTML='<select name="加工项目" id="加工项目" class="btn1" onmouseover="this.focus();" onblur="this.className=\'btn1\'" onfocus="this.className=\'input1-bor\'" dataType="Require" msg="加工项目没有选择" >'+
     '<option value=""></option>'+ str01 + '</select>' newcell=newRow.insertCell()
newcell.style.backgroundColor='#FFFFFF'
newcell.innerHTML='<input onkeypress="return   event.keyCode>=48&&event.keyCode<=57||event.keyCode==46"   onpaste="return   !clipboardData.getData('+String.fromCharCode(39)+'text'+String.fromCharCode(39)+').match(/\D/)"   style="ime-mode:Disabled"   ondragenter="return   false" name="加工数量" type="text" id="加工数量" size="9" maxlength="10" class="btn1" onmouseover="this.focus();" onblur="this.className=\'btn1\'" onfocus="this.className=\'input1-bor\'" dataType="Require" msg="加工数量没有填写" />' newcell=newRow.insertCell()
newcell.style.backgroundColor='#FFFFFF'
newcell.innerHTML='<input onkeypress="return   event.keyCode>=48&&event.keyCode<=57||event.keyCode==46"   onpaste="return   !clipboardData.getData('+String.fromCharCode(39)+'text'+String.fromCharCode(39)+').match(/\D/)"   style="ime-mode:Disabled"   ondragenter="return   false" name="预计损耗" type="text" id="预计损耗" size="9" maxlength="10" class="btn1" onmouseover="this.focus();" onblur="this.className=\'btn1\'" onfocus="this.className=\'input1-bor\'" dataType="Require" msg="预计损耗没有填写" />' newcell=newRow.insertCell()
newcell.style.backgroundColor='#FFFFFF'
newcell.innerHTML='<input onkeypress="return   event.keyCode>=48&&event.keyCode<=57||event.keyCode==46"   onpaste="return   !clipboardData.getData('+String.fromCharCode(39)+'text'+String.fromCharCode(39)+').match(/\D/)"   style="ime-mode:Disabled"   ondragenter="return   false" name="单价" type="text" id="单价" size="9" maxlength="10" class="btn1" onmouseover="this.focus();" onblur="this.className=\'btn1\'" onfocus="this.className=\'input1-bor\'"/>' newcell=newRow.insertCell()
newcell.style.backgroundColor='#FFFFFF'
newcell.innerHTML='<input name="预计交期" type="text" id="预计交期" size="10" maxlength="10" class="btn1" onmouseover="this.focus();" onblur="this.className=\'btn1\'" onfocus="this.className=\'input1-bor\';setday(this)" dataType="Require" msg="预计交期没有填写" />' newcell=newRow.insertCell()
newcell.style.backgroundColor='#FFFFFF'
newcell.innerHTML='<input name="备注" type="text" id="备注" size="20" maxlength="255" class="btn1" onmouseover="this.focus();" onblur="this.className=\'btn1\'" onfocus="this.className=\'input1-bor\'"/>' newcell=newRow.insertCell()
newcell.style.backgroundColor='#FFFFFF'
newcell.innerHTML='<input   type="button"   value="删除"   onclick="del()" >'   document.form1.c.value=c; //改变记录总数
   document.form1.button.value='现有'+c+'行,增加一行(A)';  //改变按钮值
   if(document.form1.c.value>0)document.form1.Action.disabled=false;
}   
function   del(){   
  --c;
  document.all.CaiGou.deleteRow(window.event.srcElement.parentElement.parentElement.rowIndex); 
  document.form1.c.value=c; //改变记录总数
  document.form1.button.value='现有'+c+'行,增加一行(A)';  //改变按钮值
  if(document.form1.c.value<=0)document.form1.Action.disabled=true;  for(i=1;i<=c;i++){
document.all.CaiGou.rows[i].cells[0].innerText=i
}
}

解决方案 »

  1.   


    <INPUT TYPE="button" value="添加" onclick="add()">
    <INPUT TYPE="button" value="删除" onclick="del()">
    <INPUT TYPE="button" value="提交" onclick="check()">
    <br/>
    <span id="ospan"></span>
    <SCRIPT LANGUAGE="JavaScript">
    <!--
    function add(){
    document.getElementById('ospan').innerHTML+='<input type = "text" name="s1">'
    }
    function del(){
    var o = document.getElementById('ospan');
    if(o.lastChild)
    o.removeChild(o.lastChild)
    }
    function check(){
    var sign = 0;
    var oIn = document.getElementsByTagName("input");
    for(var i=0;i<oIn.length;i++){
    if(oIn[i].type=='text' && oIn[i].name.indexOf('s')!=-1 && oIn[i].value.length<1 ){
    sign+=1;
    }
    }
    if(sign>0){
    alert('请输入')
    }else{
    alert('ok')
    }
    }
    //-->
    </SCRIPT>