js控制增加行修改版

来源:互联网 发布:阿里编程测试 编辑:程序博客网 时间:2024/05/19 03:44
页面代码
  1. <%@ page contentType="text/html; charset=GBK" %>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8.   <head>
  9.     <base href="<%=basePath%>">
  10.     
  11.     <title>教育信息增加</title>
  12.     
  13.     <meta http-equiv="pragma" content="no-cache">
  14.     <meta http-equiv="cache-control" content="no-cache">
  15.     <meta http-equiv="expires" content="0">    
  16.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  17.     <meta http-equiv="description" content="This is my page">
  18.     <!--
  19.     <link rel="stylesheet" type="text/css" href="styles.css">
  20.     -->
  21.     <script type="text/javascript" src="<%=request.getContextPath() %>/js/myjs.js"></script>
  22.     <script type="text/javascript" src="<%=request.getContextPath() %>/js/PopCalendar.js"></script>
  23.     <script type="text/javascript" src="<%=request.getContextPath() %>/js/MiniCalendar.js"></script>    
  24.   </head>
  25.   
  26.   <body>
  27.     <form id="form1" action="" method="post">
  28.       <div id="div1" style="width:760px;border:1px;border-color:#000000;border-style:solid">
  29.         <table width="760px">
  30.             <caption>教育经历</caption>
  31.             <tr>
  32.                 <td align="right">教育类型</td>
  33.                 <td align="left">
  34.                     <select id="education" style="width:140px;">
  35.                         <option value="1">小学</option>
  36.                         <option value="2">初中</option>
  37.                         <option value="3">高中</option>
  38.                         <option value="4">大学</option>
  39.                     </select>
  40.                 </td>
  41.                 <td align="right">年数</td>
  42.                 <td align="left"><input type="text" id="years" style="width:140px;"></td>
  43.             </tr>
  44.             <tr>
  45.                 <td align="right">起始日期</td>
  46.                 <td align="left"><input type="text" id="sdate" readonly="true" style="background-color:#A9BEC7;width:140px;" >
  47.                 <img style="cursor:hand;" src="<%=request.getContextPath() %>/images/image013.gif" onclick="fPopCalendar(sdate,sdate);" /></td>
  48.                 <td align="right">结束日期</td>
  49.                 <td align="left"><input type="text" id="edate" readonly="true" style="background-color:#A9BEC7;width:140px;">
  50.                 <img style="cursor:hand;" src="<%=request.getContextPath() %>/images/image013.gif" onclick="fPopCalendar(edate,edate);" /></td>
  51.             </tr>
  52.             <tr>
  53.                 <td align="right">其他</td>
  54.                 <td align="left" colsapn="2" >
  55.                 <textarea rows="3" style="width:280px;" id="other"></textarea></td>
  56.                 <td align="right"> </td>
  57.             </tr>
  58.             <tr>
  59.                 <td align="right" colspan="3"> </td>
  60.                 <td align="left">
  61.                     <input type="button" id="addbtn" value="增加" onclick="addRow()">
  62.                     <input type="button" id="savebtn" value="保存">
  63.                     <input type="reset" id="resetbtn" value="重置"
  64.                 </td>
  65.             </tr>
  66.         </table>
  67.       </div>
  68.     </form>
  69.     <form id="form2" action="" method="post">
  70.       <table width="760px" cellspacing="0" cellpadding="1" bordercolorlight="#ffffff" bordercolordark="#000000" border="1">
  71.         <caption align="left">教育信息列表</caption>
  72.         <tbody >
  73.             <tr>
  74.                 <td width="15%" align="center">教育类型</td>
  75.                 <td width="15%" align="center">年数</td>
  76.                 <td width="20%" align="center">起始日期</td>
  77.                 <td width="20%" align="center">结束日期</td>
  78.                 <td width="25%" align="center">其他</td>
  79.                 <td width="5%" align="center">操作</td>
  80.             </tr>
  81.         </tbody>
  82.         <tbody id="templetTbody">       
  83.         </tbody>
  84.       </table>
  85.     </form>
  86.   </body>
  87. </html>

相关js代码

  1. function addRow(){
  2.     if(!checkValue()){
  3.         return false;
  4.     }
  5.     var tbody=document.getElementById("templetTbody");
  6.     var newRow=tbody.insertRow();
  7.     var education=document.createElement("<input type='hidden' id='education_s'>");
  8.     var years=document.createElement("<input type='hidden' id='year_s'>");
  9.     var sdate=document.createElement("<input type='hidden' id='sdate_s'>");
  10.     var edate=document.createElement("<input type='hidden' id='edate_s'>");
  11.     var other=document.createElement("<input type='hidden' id='other_s'>");
  12.     var operate=document.createElement("<input type='button' value='删除' onclick='delRow(this)'>");
  13.     var td1=document.createElement("td");
  14.     td1.align="center";
  15.     var td2=document.createElement("td");
  16.     td2.align="center";
  17.     var td3=document.createElement("td");
  18.     td3.align="center";
  19.     var td4=document.createElement("td");
  20.     td4.align="center";
  21.     var td5=document.createElement("td");
  22.     td5.align="center";
  23.     var td6=document.createElement("td");
  24.     td6.align="center";
  25.     newRow.appendChild(td1);
  26.     newRow.appendChild(td2);
  27.     newRow.appendChild(td3);
  28.     newRow.appendChild(td4);
  29.     newRow.appendChild(td5);
  30.     newRow.appendChild(td6);
  31.     newRow.appendChild(education);
  32.     newRow.appendChild(years);
  33.     newRow.appendChild(sdate);
  34.     newRow.appendChild(edate);
  35.     newRow.appendChild(other);
  36.     td1.innerText=document.getElementById("education").options[document.getElementById("education").selectedIndex].innerText;
  37.     td2.innerText=document.getElementById("years").value;
  38.     td3.innerText=document.getElementById("sdate").value;
  39.     td4.innerText=document.getElementById("edate").value;
  40.     if(document.getElementById("other").value.length==0){
  41.         td5.innerHTML=" ";
  42.     }
  43.     else{
  44.         td5.innerText=document.getElementById("other").value;
  45.     }
  46.     td6.insertBefore(operate);
  47.     education.value=document.getElementById("education").value;
  48.     years.value=document.getElementById("years").value;
  49.     sdate.value=document.getElementById("sdate").value;
  50.     edate.value=document.getElementById("edate").value;
  51.     other.value=document.getElementById("other").value;
  52. }
  53. function delRow(obj){
  54.     var tbody=document.getElementById("templetTbody");
  55.     var tr=obj.parentNode.parentNode;
  56.     tbody.removeChild(tr);
  57. }
  58. function checkValue(){
  59.     var education=document.getElementById("education").value;
  60.     education=education.replace(/(^/s*)|(/s*$)/g, "");
  61.     var years=document.getElementById("years").value;
  62.     years=years.replace(/(^/s*)|(/s*$)/g, "");
  63.     var sdate=document.getElementById("sdate").value;
  64.     sdate=sdate.replace(/(^/s*)|(/s*$)/g, "");
  65.     var edate=document.getElementById("edate").value;
  66.     edate=edate.replace(/(^/s*)|(/s*$)/g, "");
  67.     var other=document.getElementById("other").value;
  68.     other=other.replace(/(^/s*)|(/s*$)/g, "");
  69.     if(education.length==0){
  70.         window.alert("【教育类型】不能为空!");
  71.         document.getElementById("education").focus();
  72.         return false;
  73.     }
  74.     if(years.length==0){
  75.         window.alert("【年数】不能为空!");
  76.         document.getElementById("years").focus();
  77.         return false;
  78.     }
  79.     if(isNaN(years)){
  80.         window.alert("【年数】类型输入错误!");
  81.         document.getElementById("years").focus();
  82.         return false;
  83.     }
  84.     if(sdate.length==0){
  85.         window.alert("【起始日期】不能为空!");
  86.         document.getElementyId("sdate").focus();
  87.         return false;
  88.     }
  89.     if(edate.length==0){
  90.         window.alert("【结束日期】不能为空!");
  91.         document.getElementById("edate").focus();
  92.         return false;
  93.     }
  94.     return true;
  95. }
原创粉丝点击