表格新增行,删除行,并且序号跟着变

<script src="./jquery.min.js" type="text/javascript"></script>
<style>
.inputTask input {
 width: 95%;
 height: 20px;
}

.inputhours input {
 width: 30px;
 height: 20px;
 padding-left: 20px !important;
 border: 1px solid #7F9DB9;
}

.inputhours2 input {
 width: 30px;
 height: 20px;
 padding-left: 20px !important;
 border: 1px solid white;
}

.inputhours3 input {
 width: 52px;
 height: 17px;
 padding-left: 20px !important;
 border: 1px solid white;
}

.inputpro {
 width: 130px;
 height: 17px;
 border: 1px solid white;
}

.inputHighlighted {
 background-color: #317082;
 color: #FFF;
 width: 300px;
 border: 1px solid #000;
}

.hoursContent input.focus {
 outline: solid 1px #FF942B;
}

.inputsave {
 background:
  url("/html/portlet/ext/XAprojectManage/style/images/operation/save.jpg");
 background-position: 5px 1px;
 background-repeat: no-repeat;
}

.inputpass {
 background:
  url("/html/portlet/ext/XAprojectManage/style/images/operation/pass.jpg");
 background-position: 5px 1px;
 background-repeat: no-repeat;
}

.inputfail {
 background:
  url("/html/portlet/ext/XAprojectManage/style/images/operation/fail.jpg");
 background-position: 5px 1px;
 background-repeat: no-repeat;
}

.inputwait {
 background:
  url("/html/portlet/ext/XAprojectManage/style/images/operation/wait.jpg");
 background-position: 5px 1px;
 background-repeat: no-repeat;
}

.linetop {
 border-top: 1px solid #000099;
}

.lineright {
 border-right: 1px solid #000099;
}

.linebottom {
 border-bottom: 1px solid #000099;
}

.lineleft {
 border-left: 1px solid #000099;
}

table {
 border-collapse: collapse;
 border-spacing: 0;
 clear: both;
}
</style>

 

<div style="margin-top:6px;">
<input type="button" value="添加行" οnclick="addcur();"
 class="approve_btn" />
</div>
<table width="100%" name="tableContent" id="tableContent" style="margin-top:12px;"
 class="HOURS" cellpadding="0" cellspacing="0">
 <tr height="35" id="THday" align="center">
  <td width="5%" class="linebottom linetop lineleft"
   style="background-color: #E6EDFD;">&nbsp;</td>
  <td width="5%" class="linebottom linetop "
   style="background-color: #E6EDFD;" align="center">主题</td>
  <td width="18%" class="linebottom linetop lineleft"
   style="background-color: #E6EDFD;" align="center">主题<font color="red">*</font></td>
  <td width="13%" class="linebottom lineright linetop lineleft"
   style='background-color: #E6EDFD;'>主题<font color="red">*</font></td>
  <td width="23%" class="linebottom linetop lineleft lineright"
   style="background-color: #E6EDFD;">主题<font color="red">*</font></td>
  <td width="8%" class="linebottom lineright linetop"
   style='background-color: #E6EDFD;'>主题</td>
  <td width="10%" class="linebottom lineright linetop"
   style='background-color: #E6EDFD;'>主题</td>
 </tr>
 <tbody id="clonetab">
  <tr align="center">
   <td class="linebottom linetop lineleft"></td>
   <td class="linebottom linetop "><input type="hidden" name="questionNo" id="questionNo1" value="1"/>1</td>
   <td class="linebottom linetop lineleft"><textarea style='margin:10px;' cols="50" rows="3" name="questionDesc"
    id="questionDesc1" οnkeyup="deleteValidationErrorMassage('questionDesc1');"></textarea></td>
   <td class="linebottom linetop lineleft"><select id="questionType1"
    name="questionType" οnchange="deleteValidationErrorMassage('questionType1');changeType(this,'questionType1');">
    <option value="">--请选择--</option>
    <option value="radio">单选</option>
    <option value="checkbox">多选</option>
    <option value="other">其他</option>
   </select></td>
   <td class="linebottom lineright linetop lineleft">
   <table id="answer1">
    <tr align="center">
     <td > <img
      src="./2.jpg"
      οnclick="doplus(this);" alt="44" title="44" style='cursor:pointer;'/><!--  <input  type="button" class="plus" οnclick="doplus(this);" value="" />--></td>
     <td><input type='hidden' value='65' name='alpha' id='answer1alpha1'/><input type='hidden' value="1" name='currRow'/>A</td>
     <td><input type="text"  width='128px;' name="answerOption" id="answer1Option1"
      οnkeyup="deleteValidationErrorMassage('answer1Option1');" /></td>
     <td width='25px'>&nbsp;</td>
    </tr>
    <tr>
     <td>&nbsp;</td>
     <td><input type='hidden' value='66' name='alpha' id='answer1alpha2'/><input type='hidden' value="1" name='currRow'/>B</td>
     <td><input type='hidden' value="2" name='currRow'/><input type="text" width="128px" name="answerOption" id="answer1Option2"
      οnkeyup="deleteValidationErrorMassage('answer1Option2');" /></td>
     <!-- <td><input type="button" class="minus" οnclick="dominus(this);" name="answerOption"/></td> -->
       <td><img style='margin-left:3px;cursor:pointer;' src='./1.jpg' οnclick='dominus(this);'  alt='33' title='33'/></td>
    </tr>
   </table>
   </td>
   <td class="linebottom lineright linetop"><input type="hidden" name="requiredHidden" id="requiredHidden1" value="0"/><input type="checkbox" name="required" id="required1" value="0" οnclick="if(this.checked){jQuery('#requiredHidden1').val('1');}else{jQuery('#requiredHidden1').val('0');}"/></td>
   <td class="linebottom lineright linetop"><input type="hidden" name="inputBoxRequiredHidden" id="inputBoxRequiredHidden1"  value="0"/><input type="checkbox" name="inputBoxRequired" id="inputBoxRequired1" value="0" οnclick="if(this.checked){jQuery('#inputBoxRequiredHidden1').val('1');}else{jQuery('#inputBoxRequiredHidden1').val('0');}"/></td>
  </tr>
 </tbody>
</table>
</form>
</div>
<br/>
<script type="text/javascript">


function newRows(){
 
 var resultId = 0;
 jQuery("input[name='questionNo']").each(function(){
  var value1 = jQuery(this).val();
  if(value1 != "" && value1 != null){
   if(parseInt(value1) > parseInt(resultId))
    resultId = value1; 
  }
 });
 var newId=0;
 if(parseInt(resultId)!=0){
  newId=parseInt(resultId)+ 1;
 }
 return newId;
}

function addcur(){
   //var sumrow=rownum();
      var newId=newRows();
  
  var html="<tr height='35' align='center'>";
  html+="<td align='center'width='5%' class='linebottom lineleft'><img title='删除行' οnclick='deleteRow(this)' alt='删除' src='./1.jpg' style='cursor: hand;'></td>";
  html+="<td width='5%' class='linebottom linetop'><input type='hidden' name='questionNo' id='questionNo"+newId+"' value='"+newId+"'/>"+newId+"</td>";
  html+="<td width='18%' class='linebottom lineleft'><textarea  cols='50' rows='3' style='margin:10px;' name='questionDesc' id='questionDesc"+newId+"' οnkeyup='deleteValidationErrorMassage(\"questionDesc"+newId+"\");'></textarea></td>";
  html+="<td width='13%' class='linebottom linetop lineleft'><select  name='questionType' id='questionType"+newId+"' οnchange='changeType(this,\"questionType"+newId+"\");deleteValidationErrorMassage(\"questionType"+newId+"\");'><option value=''>--请选择 --</option>"
   +"<option value='radio'>单选</option><option value='checkbox'>多选</option><option value='other'>其他</option></select></td>";
  html+="<td width='23%' class='linebottom linetop lineleft'><table id='answer"+newId+"' name='answer'><tr align='center' ><td><img src='./2.jpg' style='cursor:pointer;' οnclick='doplus(this);' alt='999' title='99' /></td><td><input type='hidden' value='1' name='currRow'/><input type='hidden' value='65' name='alpha' id='answer"+newId+"alpha1'/>A</td><td><input type='text'  width='128px;' name='answerOption' id='answer"+newId+"Option1' οnkeyup='deleteValidationErrorMassage(\"answer"+newId+"Option1\");'/></td><td width='25px'>&nbsp;</td></tr><tr><td>&nbsp;</td><td><input type='hidden' value='2' name='currRow'/><input type='hidden' value='66' name='alpha' id='answer"+newId+"alpha2'/>B</td><td><input type='text' witdh='128px' name='answerOption' name='answerOption' id='answer"+newId+"Option2' οnkeyup='deleteValidationErrorMassage(\"answer"+newId+"Option2\");'/></td> <td><img style='margin-left:3px;cursor:pointer;' src='./1.jpg' οnclick='dominus(this);'  alt='99' title='99'/></td></tr></table></td>";
  html+="<td width='8%' class='linebottom linetop lineleft'><input type='hidden' name='requiredHidden' id='requiredHidden"+newId+"' value='0'/><input type='checkbox' name='required' id='required"+newId+"' value='0' οnclick='changeValueRequired(this,\"requiredHidden"+newId+"\");'/></td>";
  html+="<td width='10%' class='linebottom lineleft lineright'><input type='hidden' name='inputBoxRequiredHidden' id='inputBoxRequiredHidden"+newId+"' value='0'/><input type='checkbox' name='inputBoxRequired' id='inputBoxRequired"+newId+"' value='0' οnclick='changeValueRequired(this,\"inputBoxRequiredHidden"+newId+"\");'/></td>";
  jQuery(html).appendTo(jQuery("#clonetab"));
        
 }

//\u63a7\u5236\u9875\u9762\u6700\u5927\u884c\u6570
function rownum(){
 //\u83b7\u5f97\u9875\u9762\u4e0a\u663e\u793a\u7684\u884c\u6570
 var pageRow = document.getElementsByName("questionDesc");
 var sumrow=pageRow.length;
 return sumrow;
}

//\u5220\u9664\u884c
function deleteRow(obj){
 var pageRow = document.getElementsByName("questionDesc");
 if(pageRow.length==1){
  alert("\u65e0\u6cd5\u5220\u9664\u4ec5\u6709\u6b64\u884c\uff01");
 }else{
  var rowss=jQuery(obj).parent('td').parent('tr')[0].rowIndex;
  var rowSize = jQuery('#tableContent > tbody:nth-child(2) > tr').size();
  for (var i = Number(rowss); i <= Number(rowSize); i ++) {
   html="<tr height='35' align='center'>";
   html+="<td align='center' width='5%' class='linebottom lineleft'><img title='删除行' οnclick='deleteRow(this)' alt='3' src='./1.jpg' style='cursor: hand;'></td>";
   html+="<td width='5%' class='linebottom linetop'><input type='hidden'  name='questionNo' value='"+i+"'/>"+i+"</td>";
   var oldquestionDesc = jQuery('#tableContent > tbody:nth-child(2) > tr:nth-child('+(i+1)+') > td:nth-child(3) > textarea:first-child').val();
   html+="<td width='18%' class='linebottom lineleft'><textarea  cols='50' rows='3' style='margin:10px;' name='questionDesc' id='questionDesc"+i+"' οnkeyup='deleteValidationErrorMassage(\"questionDesc"+i+"\");'>"+oldquestionDesc+"</textarea></td>";
   var oldquestionTypeVal = jQuery('#tableContent > tbody:nth-child(2) > tr:nth-child('+(i+1)+') > td:nth-child(4) > select:first-child').val();
   var oldquestionType = "<option value=''>--单选 --</option><option value='radio'";
   if ("radio"==oldquestionTypeVal) {
    oldquestionType+=" selected";
   }
   oldquestionType+=" >多选</option><option value='checkbox'";
   if ("checkbox"==oldquestionTypeVal) {
    oldquestionType+=" selected";
   }
   oldquestionType+=" 其他</option><option value='other'";
   if ("other"==oldquestionTypeVal) {
    oldquestionType+=" selected";
   }
   oldquestionType+=" >\u5176\u4ed6</option>";
   html+="<td width='13%' class='linebottom linetop lineleft'><select name='questionType' id='questionType"+i+"' οnchange='deleteValidationErrorMassage(\"questionType"+i+"\");changeType(this,\"questionType"+i+"\");'>"
    +oldquestionType+"</select></td>";
   html+="<td width='23%' class='linebottom linetop lineleft'><table id='answer"+i+"' name='answer'>";
   if ("other"!=oldquestionTypeVal) {
    var ansSize = jQuery('#tableContent > tbody:nth-child(2) > tr:nth-child('+(i+1)+') > td:nth-child(5) > table:first-child > tbody:first-child > tr').size();
    for (var j = 1; j <= Number(ansSize); j++) {
     var currRow = jQuery('#tableContent > tbody:nth-child(2) > tr:nth-child('+(i+1)+') > td:nth-child(5) > table:first-child > tbody:first-child > tr:nth-child('+j+') > td:nth-child(2) > input[name="currRow"]').val();
     var alpha = jQuery('#tableContent > tbody:nth-child(2) > tr:nth-child('+(i+1)+') > td:nth-child(5) > table:first-child > tbody:first-child > tr:nth-child('+j+') > td:nth-child(2) > input[name="alpha"]').val();
     var ans = jQuery('#tableContent > tbody:nth-child(2) > tr:nth-child('+(i+1)+') > td:nth-child(5) > table:first-child > tbody:first-child > tr:nth-child('+j+') > td:nth-child(3) > input[name^="answerOption"]').val();
     html+="<tr align='center'><td>";
     if (j == 1) {
      html+="<img src='./1.jpg' style='cursor:pointer;' οnclick='doplus(this);' alt='\u589e\u52a0\u7b54\u6848' cursor='pointer'/>";
     }
     html+="</td><td><input type='hidden' value='"+currRow+"' name='currRow'/><input type='hidden' value='"+alpha+"' name='alpha' id='answer"+i+"alpha"+j+"'/>"+String.fromCharCode(alpha)+"</td><td><input value='"+ans+"' type='text' width='128px' name='answerOption' id='answer"+i+"Option"+j+"' οnkeyup='deleteValidationErrorMassage(\"answer"+i+"Option"+j+"\");'/></td><td width='25px'>";
     if (j >= 2) {
      html+="<img src='./1.jpg' style='cursor:pointer;' οnclick='dominus(this);' cursor='pointer' alt='\u5220\u9664'/>";
     }
     html+="</td></tr>";
    }
   }
   html+="</table></td>";
   var isReq = jQuery('#tableContent > tbody:nth-child(2) > tr:nth-child('+(i+1)+') > td:nth-child(6) > input[name="required"]').is(':checked');
   var reqStr = isReq == true ? 'checked' : '';
   html+="<td width='8%' class='linebottom linetop lineleft'><input "+reqStr+" type='checkbox' id='required"+i+"' name='required' value='0' οnclick='changeValueRequired(this,\"requiredHidden"+i+"\");'/> <input type='hidden' name='requiredHidden' id='requiredHidden"+i+"' value='0' /></td>";
   var isBox = jQuery('#tableContent > tbody:nth-child(2)> tr:nth-child('+(i+1)+') > td:nth-child(7) > input[name="inputBoxRequired"]').is(':checked');
   var boxStr = isBox == true ? 'checked' : '';
   html+="<td width='10%' class='linebottom lineleft lineright'><input "+boxStr+" type='checkbox' id='inputBoxRequired"+i+"' name='inputBoxRequired' value='0' οnclick='changeValueRequired(this,\"inputBoxRequiredHidden"+i+"\");'/> <input type='hidden' name='inputBoxRequiredHidden' id='inputBoxRequiredHidden"+i+"' value='0' /></td>";
   html+="</tr>";
   jQuery('#tableContent > tbody:nth-child(2) > tr:nth-child('+(i+1)+')').replaceWith(html);
  }
  jQuery(obj).parent('td').parent('tr').remove();
 }  
}


function doplus(obj){
 
 var cTr = jQuery(obj).parent('td').parent('tr').parent('tbody').parent('table').parent('td').parent('tr');
 var trId = cTr.get(0).rowIndex;
 var pageRow = jQuery('#answer'+trId+' tbody:nth-child(1) tr').size();
  if(pageRow>=10){
    alert("超过10行");
  }else{
 var colvar;
 var colam;
 var tt;
 var currRow=2;//\u91cc\u9762Table\u5f53\u524d\u884c\u53f7;\u5373\uff1a\u6bcf\u884c\u9009\u9879A\u4e3a\u7b2c\u4e00\u884c\uff0c\u9009\u9879B\u4e3a\u7b2c\u4e8c\u884c\uff0c\u9012\u589e
 jQuery("table#answer"+trId+" input[name='alpha']").each(function(){
   tt = jQuery(this).val();
 });
 if(tt==undefined||tt==""){
  colvar=67;
 }else{
  colvar=Number(tt)+1;
 }
 jQuery("table#answer"+trId+" input[name='currRow']").each(function(){
  currRow = jQuery(this).val();
 });
 currRow=Number(currRow)+1;
 var html="<tr><td>&nbsp;</td>";
  html+="<td><input type='hidden' value="+currRow+" name='currRow'/><input type='hidden' value="+colvar+" name='alpha' id='answer"+trId+"alpha"+currRow+"'/>"+ String.fromCharCode(colvar)+"</td><td><input type='text' name='answerOption' id='answer"+trId+"Option"+currRow+"'/></td>";
  html+="<td width='25px'><img style='margin-left:3px;cursor:pointer;' src='./1.jpg' οnclick='dominus(this);'  alt='33' title='33'/></td>";
 jQuery(html).appendTo(jQuery('#answer'+trId+''));
   }
}
//\u5220\u9664\u91cc\u9762Table\u884c
function dominus(obj){
 var pageRow = document.getElementsByName("alpha");
 if(pageRow.length==0){
  alert("\u65e0\u6cd5\u5220\u9664\u4ec5\u6709\u6b64\u884c\uff01");
 }else{
  
  var cTr = jQuery(obj).parent('td').parent('tr').parent('tbody').parent('table').parent('td').parent('tr');
  var rowss = cTr.get(0).rowIndex;//\u5916\u9762Table\u5f53\u524d\u884c\u53f7
  var cTr2 = jQuery(obj).parent('td').parent('tr');
  var trId=cTr2.get(0).rowIndex;//\u91cc\u9762Table\u5f53\u524d\u884c\u53f7
  var oldquestionTypeVal=jQuery("#questionType"+rowss+"").val();
      var rowSize = jQuery('#answer'+rowss+' tbody:nth-child(1) tr').size();//\u91cc\u9762Table\u603b\u957f\u5ea6
  if ("other"!=oldquestionTypeVal) {
   for (var j = Number(trId)+1; j <Number(rowSize); j++) {
     var html="";
     var currRow =j;
     var alpha = jQuery('#answer'+rowss+'alpha'+currRow+'').val();
     var ans = jQuery('#answer'+rowss+'Option'+(currRow+1)+'').val();
     html+="<tr align='center'><td>";
     if (currRow == 1) {
      html+="<img src='./1.jpg' style='cursor:pointer;' οnclick='doplus(this);' alt='\u589e\u52a0\u7b54\u6848' cursor='pointer'/>";
     }
     html+="</td><td><input type='hidden' value='"+currRow+"' name='currRow'/><input type='hidden' value='"+alpha+"' name='alpha' id='answer"+rowss+"alpha"+currRow+"'/>"+String.fromCharCode(alpha)+"</td><td><input value='"+ans+"' type='text'  width='128px;' name='answerOption' id='answer"+rowss+"Option"+currRow+"' οnkeyup='deleteValidationErrorMassage(\"answer"+rowss+"Option"+currRow+"\");'/></td><td width='25px'>";
     if (currRow >= 2) {
      html+="<img src='./1.jpg' style='cursor:pointer;' οnclick='dominus(this);' cursor='pointer' alt='\u5220\u9664'/>";
     }
     html+="</td></tr>";
     jQuery('#answer'+rowss+' tbody:first-child tr:nth-child('+j+')').replaceWith(html); 
    }
  }
  
  jQuery('#answer'+rowss+' tbody:last-child tr:last-child').remove();
 }
 
}


function countAnswerNum(){
 var answerNumString="";
 var sum=rownum();
 for(var i=1;i<=Number(sum);i++){
  var rowTableLength=document.getElementById('answer'+i+'').rows.length;
  answerNumString+=rowTableLength+",";
 }
 return answerNumString;  
}


</script>

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值