多个下拉列表(select)不能取重复值判断-动态增加输入框-模仿CSDN结贴时分数判断-但无层提示-Ajax取值(全部源码)

来源:互联网 发布:淘宝网三星a9手机壳 编辑:程序博客网 时间:2024/05/22 10:43
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4.     <title>动态增删表格行</title>
  5.     <script language="JavaScript">
  6. var request = false;
  7. var rowIndex=0;
  8. var Strs="";
  9. var Values="";
  10.   
  11.    function getCustomerInfo() {
  12.    
  13.      var url = "/Ajax/lookupCustomer.jsp?";
  14.      request.open("GET", url, true);
  15.      request.onreadystatechange = updatePage;
  16.      request.send(null);
  17.      }
  18. function updatePage() {
  19.      if (request.readyState == 4)
  20.        {if (request.status == 200)
  21.          {
  22.     
  23.           var res1=request.responseXML.getElementsByTagName("res")
  24.         var subMenu1 = "----请选择题型----";
  25.         
  26.         for(var i = 0; i < res1.length; i++) {
  27.         
  28.             subMenu1subMenu1 = subMenu1+ "," + res1[i].firstChild.data + "";
  29.         }
  30.         
  31.     StrssubMenu1.split(",");
  32.     
  33.     var res2=request.responseXML.getElementsByTagName("val")
  34.         var subMenu2"-1";
  35.         for(var j0; j < res2.length; j++) {
  36.            subMenu2subMenu2 = subMenu2+ " , " + res2[j].firstChild.data + "";
  37.        }
  38.         Values = subMenu2.split(",");
  39.          }
  40.        else if (request.status == 404)
  41.         { alert("Request URL does not exist");}
  42.          else if (request.status == 403) {
  43.          alert("Access denied.");
  44.        } 
  45.        else
  46.          alert("Error: status code is " + request.status);
  47.          }
  48.    }     
  49.     
  50. function AddOption(Values,Strs)
  51. {
  52. var i;
  53. var MyOption="";
  54. for (i=0;i<Values.length;i++)
  55. {
  56. MyOptionMyOption=MyOption+"<option value="+Values[i]+">"+Strs[i]+"</option>";
  57. }
  58. return MyOption;
  59. }
  60. function createXMLHttpRequest() {
  61. try {
  62.      request = new XMLHttpRequest();
  63.    } catch (trymicrosoft) {
  64.      try {
  65.     
  66.        request = new ActiveXObject("Msxml2.XMLHTTP");
  67.      } catch (othermicrosoft) {
  68.        try {
  69.       
  70.          request = new ActiveXObject("Microsoft.XMLHTTP");
  71.        } catch (failed) {
  72.          request = false;
  73.        }  
  74.      }
  75.    }
  76.    if (!request)
  77.      alert("Error initializing XMLHttpRequest!");
  78.      
  79. }
  80. function addLine(obj){
  81. var objobjSourceRow=obj.parentNode.parentNode;
  82. var objobjTable=obj.parentNode.parentNode.parentNode.parentNode;
  83. if(obj.value=='增加'){
  84. createXMLHttpRequest();
  85. getCustomerInfo();
  86. rowIndex++;
  87. var objRow=objTable.insertRow(rowIndex);
  88. var objCell;
  89. objCell=objRow.insertCell(0);
  90. objCell.innerHTML="<input name='row"+rowIndex+"' value = '第"+rowIndex+"题'>"//在表格单元中添加文本输入框
  91. objCell=objRow.insertCell(1);
  92. objCell.innerHTML="<select name='tixing' id='"+rowIndex+"'onChange='checkTixing(this)'>"+AddOption(Values,Strs)+"</select>";
  93. objCell=objRow.insertCell(2);
  94. objCell.innerHTML="<input name='"+rowIndex+"' value = ''>"
  95. objCell=objRow.insertCell(3);
  96. objCell.innerHTML="<input name='tb_score' value = '' id='point'>";
  97. objCell=objRow.insertCell(4);
  98. objCell.innerHTML=objSourceRow.cells[4].innerHTML.replace(/增加/,'删除');
  99. }
  100. else{
  101. objTable.lastChild.removeChild(objSourceRow);
  102. rowIndex--;
  103. }
  104. function checkTixing(obj){
  105.  var  tixing=document.getElementsByName('tixing');
  106.     
  107.     for(var i = 0;i<tixing.length;i++)
  108.     {
  109.          if(obj!=tixing[i] && obj.value==tixing[i].value)
  110.          {
  111.             alert("题型重复了,请选择另一个!");
  112.             return;
  113.          }
  114.         
  115.     }
  116. }
  117. function checkPoint(){
  118. //var points = document.getElementById("point").innerHTML;
  119. var points=20;
  120. var txts = document.getElementsByName("tb_score");
  121. //alert("txts.length:"+txts.length);
  122. for(var i=0;i<txts.length;++i)
  123. {
  124.   
  125.     var score = new Number(txts[i].value);
  126.     
  127.     if (score<0)
  128.     {
  129.         alert("不能给负分");
  130.         focusOnPoint(txts[i]);
  131.         return false;
  132.     }
  133.      if (score==0)
  134.     {
  135.         alert("不能不给分");
  136.         focusOnPoint(txts[i]);
  137.         return false;
  138.     }
  139.     if (score >0)
  140.     {
  141.         points-=score;
  142.         continue;
  143.     }
  144.     
  145. }//end for
  146. if(points!=0){
  147.     alert("给分与总分不符,请重新分配!");
  148. if(txts.length>0)focusOnPoint(txts[0]);
  149. return false;
  150. }
  151. return confirm('你确定要提交吗?')
  152. }
  153. function focusOnPoint(tb){
  154. tb.style.backgroundColor="red";
  155. tb.onchange=function(){tb.style.backgroundColor="white";}
  156. tb.focus();
  157. }
  158.     </script>
  159. </head>
  160. <body bgcolor="#ffffff" onLoad="createXMLHttpRequest();getCustomerInfo();">
  161.     <table align="center">
  162.         <tr align="center">
  163.             <td width="100">
  164.                 大题号
  165.             </td>
  166.             <td width="100">
  167.                 题型
  168.             </td>
  169.             <td width="100">
  170.                 小题量
  171.             </td>
  172.             <td width="100">
  173.                 分值
  174.             </td>
  175.             <td width="100">
  176.             </td>
  177.         </tr>
  178.     </table>
  179.     <form name="form1" action="menujsp.jsp" method="post">
  180.     <table border="0" align="center">
  181.         <tr>
  182.             <td width="100">
  183.             </td>
  184.             <td width="100">
  185.             </td>
  186.             <td width="100">
  187.             </td>
  188.             <td width="100">
  189.             </td>
  190.             <td width="100">
  191.                 <input name="add" type="button" id="add" value="增加" onclick="addLine(this)"/>
  192.             </td>
  193.         </tr>
  194.         <tr>
  195.             <td> 
  196.                 
  197.             </td>
  198.             <td> 
  199.                 
  200.             </td>
  201.             <td> 
  202.                 
  203.             </td>
  204.         </tr>
  205.         <tr>
  206.             <td> 
  207.             如果可用分分配完毕,请点击
  208.                 <a onclick="return checkPoint();" href="Google-Search.html">确认结贴</a>
  209.             </td>
  210.             <td><label>
  211.                                问题点数
  212.             </label>            </td>
  213.             <td><div id="point" style="background: yellow; border: solid 1px red"><p>20</p></div>
  214.                 
  215.             </td>
  216.         </tr>
  217.     </table>
  218.     </form>
  219. </body>
  220. </html>

 

 

数据页面

lookupCustomer.jsp

 

 

 

  1. <%@ page contentType="text/html; charset=GBK" %>
  2. <
  3. String contents1="单选题"
  4. String contents2="多选题"
  5. String value1="1";  
  6. String value2="2";  
  7. //传回响应数据   
  8. response.setContentType("text/xml; charset=UTF-8"); 
  9. response.setHeader("Cache-Control", "no-cache"); 
  10. out.println("<response>");
  11. out.println("<res>" + contents1 + "</res>");
  12. out.println("<res>" + contents2 + "</res>"); 
  13. out.println("<val>" + value1 + "</val>");
  14. out.println("<val>" + value2 + "</val>"); 
  15. out.println("</response>"); 
  16.  out.close();
  17.             
  18.  %>