AJAX实例入门

http://www.webjx.com  更新日期:2006-02-27  网页教学网  浏览次数:

  最后是JSP文件:

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 <HTML>
 <HEAD>
 <TITLE>FB</TITLE>
 <LINK href="images/magazin.css" type=text/css rel=stylesheet>
 <LINK href="images/briefing.css" type=text/css rel=stylesheet>
 <META http-equiv=Content-Type content="text/html; charset=utf-8">
 <META content="MSHTML 6.00.2800.1498" name=GENERATOR>
 <style type="text/css">
 <!--
 .style1 {
  color: #000000;
  font-weight: bold;
  font-size: 10pt;
 }
 .style2 {font-size: 10pt; color: #FF0000;}
 -->
 </style>
 <script language="javascript">
 function Add()
 {
 if(document.all.Education2.style.display=='block')document.all.Education2.style.display='none';
 else document.all.Education2.style.display='block';
 }
 </script>
 </HEAD>
 <BODY leftMargin=0 topMargin=0 marginheight="0" marginwidth="0">
 <script type="text/javascript" src="images/head.js"></script>
 <script language=javascript>
 
 function getResult(type,id) {
       var url = "ajax/select.do?type="+type+"&id="+id;
       if (window.XMLHttpRequest) {
               req = new XMLHttpRequest();
       }else if (window.ActiveXObject) {
               req = new ActiveXObject("Microsoft.XMLHTTP");
       }
       if(req){
               req.open("GET",url, true);
               req.onreadystatechange = complete;
               req.send(null);
       }
 }
 function complete(){
       if (req.readyState == 4) {
               if (req.status == 200) {
                       var type = req.responseXML.getElementsByTagName("type");
                        var value = req.responseXML.getElementsByTagName("value");
                        var str=new Array();
                        for(var i=0;i<value.length;i++){
                              str[i]=value[i].firstChild.data;
                           }
                       var type = type[0].firstChild.data;
                       
                       removeType1(type);
                           
                      if(type=="0")
                       {
                        
                        buildSelect(str,document.getElementById("select28"));
                      }
                      else if(type=="1")
                      {
                        buildSelect(str,document.getElementById("select32"));
                      }
                      else if(type=="2")
                      {
                        buildSelect(str,document.getElementById("select34"));
                      }
                      
               }
       }
 }
 function buildSelect(str,sel) {
       sel.options.length=0;
       for(var i=0;i<str.length;i++) {
               sel.options[sel.options.length]=new Option(str[i],str[i])
       }
 }
         
  function removeType1(type)
   {
  var form = document.forms[0];
  if(type=="0")
  {
   removeAll(form.select28);
   removeAll(form.select29);
   removeAll(form.select30);
   removeAll(form.select31);
   removeAll(form.select32);
   removeAll(form.select33);
   removeAll(form.select34);
   removeAll(form.select35);
  }
  else if(type=="1")
  {
   removeAll(form.select31);
   removeAll(form.select32);
   removeAll(form.select33);
   removeAll(form.select34);
   removeAll(form.select35);
  }
  else if(type=="2")
  {
   removeAll(form.select34);
   removeAll(form.select35);
  }
  else if(type=="3")
  {
   
  }
   }
    
  function selChange(f,h)
   {
  for(i = 0;i < f.length; i++)
  { 
   if(f.options[i].selected == true)
   {  
    h.options.add(new Option( f.options[i].text,f.options[i].value));
    f.options.remove(i);
    i--;
   }
  }
   }
  
   function removeAll(obj)
   {
  for(i=0;i<obj.length;i++)
  {
   obj.options.remove(i);
   i--;
  }
   }
  
   function addAll(f,h)
   {
  for(i = 0;i < f.length; i++)
  {
   h.options.add(new Option( f.options[i].text,f.options[i].value));
   f.options.remove(i);
   i--;
  }
   }
  
   function changeSel(offer,getter,type)
   {
  removeType(type);
  for(i=0;i<getter.length;i++)
  {
   getter.options.remove(i);
   i--;
  }
  getter.options.add(new Option("",""));
  for(i = 0;i < offer.length; i++)
  {
   getter.options.add(new Option(offer.options[i].text,offer.options[i].value));
  }
   }
  
   function removeType(type)
   {
  var form = document.forms[0];
  if(type=="0")
  {
   removeAll(form.select25);
   removeAll(form.select28);
   removeAll(form.select29);
   removeAll(form.select30);
   removeAll(form.select31);
   removeAll(form.select32);
   removeAll(form.select33);
   removeAll(form.select34);
   removeAll(form.select35);
  }
  else if(type=="1")
  {
   removeAll(form.select30);
   removeAll(form.select31);
   removeAll(form.select32);
   removeAll(form.select33);
   removeAll(form.select34);
   removeAll(form.select35);
  }
  else if(type=="2")
  {
   removeAll(form.select31);
   removeAll(form.select34);
   removeAll(form.select35);
  }
  else if(type=="3")
  {
   
  }
   }
   function removeAll(obj)
   {
  for(i=0;i<obj.length;i++)
  {
   obj.options.remove(i);
   i--;
  }
   }
 
  </script>
 <form>
 <font face="Arial, Helvetica, sans-serif"></font>
 <TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
   <TBODY>
   <TR>
   <TD align=middle vAlign=top bgcolor="#eeeeee"><!--[ Left & Contents & Right ]------>
       <TABLE cellSpacing=0 cellPadding=0 width=945 align=center
     background=images/bg2.gif border=0>
         <TBODY>
           <TR>
             <TD vAlign=top> <TABLE cellSpacing=0 cellPadding=0 width="95%" align=center border=0>
                 <TBODY>
                   <TR>
                     <TD width="97%" align=right> <p align="left"> Resource Management&nbsp;&gt;&gt;&nbsp;New
                         Staff</TD>
                   </TR>
                   <TR height=4>
                     <TD></TD>
                   </TR>
                   <TR>
                     <TD valign="top"> <table class=homeBody cellspacing=0 cellpadding=0 width="100%"
 align=center border=0>
                         <tbody>
                           <tr>
                             <td height=25 colspan="2" nowrap><table class=grid style="WIDTH: 100%;" cellspacing=0 cellpadding=3>
                                 <tbody>
                                   <tr class=gridHeader>
                                     <td width="26%" align=left bgcolor="#eeeeee" ><strong>&nbsp;Personal
                                       Information</strong></td>
                                     <td width="74%" align=left bgcolor="#eeeeee" ><span class="gridAlternating">Last
                                       Modified : 06/06/2005</span></td>
                                   </tr>
                                 </tbody>
                               </table></td>
                           </tr>
                           <tr>
                             <td valign=middle nowrap><div align="right"><input style="width:50px" class="button" type="button" value="Save"
        name="B323252">&nbsp;&nbsp;<input style="width:50px" class="button" type="button" value="Undo" name="B323262">
                               </div></td>
                           </tr>
                           <tr>
                             <td colspan=2 valign="top" class="modifiedRow modifiedProducts">
                               <table id=DynamicTabsTable cellspacing=0 cellpadding=0
 border=0 name="DynamicTabsTable">
                                 <tbody>
                                   <tr id=DynamicTabsTR>
                                     <script language=JavaScript
 src="images/Public.js"></script>
                                   </tr>
                                 </tbody>
                               </table>
                               <table width="100%" border="0" cellpadding="0" cellspacing="3" bordercolorlight="#C0C0C0" bordercolordark="#FFFFFF">
                                 
                                 <tr>
                                   <td nowrap><fieldset align="left" style="PADDING-RIGHT: 8px; PADDING-LEFT: 8px; PADDING-BOTTOM: 8px; LINE-HEIGHT: 30px; PADDING-TOP: 8px;width:900px">
                                     <legend class=legend>Freeborders Experience</legend>
                                     <table width="100%" height="15" border="0" align="center" cellspacing="0" bordercolorlight="#C0C0C0" bordercolordark="#FFFFFF" id="table21">
                                       <tr>
                                         <td nowrap><strong>FB Vertical Experience</strong></td>
                                         <td colspan="2"><strong>FB Customer
                                           Experience </strong></td>
                                       </tr>
                                       <tr>
                                         <td height="26" valign="top" nowrap>&nbsp;</td>
                                         <td colspan="2" valign="top" nowrap><strong>
                                           <select name="select25" id="select25" style="WIDTH: 179px" onchange="getResult('0',document.forms[0].select25.value);">
                                             <option value="b">b</option>
                                             <option value="c">c</option>
                                           </select>
                                           </strong></td>
                                       </tr>
                                       <tr>
                                         <td height="26" valign="top" nowrap>
                                           <table  border="0" cellspacing="0" cellpadding="0" id="table34">
                                             <tr>
                                               <td> <select id="select26" style="WIDTH: 180px; HEIGHT: 120px" multiple="multiple" size="4" name="select26">
                                                   <option value="a">a</option>
                                                 </select>
                                               </td>
                                               <td width="50" align="middle" valign="middle" bordercolorlight="#C0C0C0" bordercolordark="#FFFFFF" class="tableblackborder"><div align="center">
                                                   <table width="80%" height="110" border="0" align="center" cellpadding="0" cellspacing="0" id="table35">
                                                     <tr>
                                                       <td><div align="center">
                                                           <input name="button2" type="button" class="button" id="button29" style="WIDTH: 30px"  value=">" onclick="selChange(document.forms[0].select26,document.forms[0].select27);changeSel(document.forms[0].select27,document.forms[0].select25,'0')" />
                                                         </div></td>
                                                     </tr>
                                                     <tr>
                                                       <td><div align="center">
                                                           <input name="button2" type="button" class="button" id="button30" style="WIDTH: 30px" value=">>" causesvalidation="False" onclick="addAll(document.forms[0].select26,document.forms[0].select27);changeSel(document.forms[0].select27,document.forms[0].select25,'0')"/>
                                                         </div></td>
                                                     </tr>
                                                     <tr>
                                                       <td height="5"><div align="center"></div></td>
                                                     </tr>
                                                     <tr>
                                                       <td><div align="center">
                                                           <input name="button2" type="button" class="button" id="button31" style="WIDTH: 30px" value="<" causesvalidation="False" onclick="selChange(document.forms[0].select27,document.forms[0].select26,document.forms[0].select25,1);changeSel(document.forms[0].select27,document.forms[0].select25,'0')"/>
                                                         </div></td>
                                                     </tr>
                                                     <tr>
                                                       <td><div align="center">
                                                           <input name="button2" type="button" class="button" id="button32" style="WIDTH: 30px" value="<<" causesvalidation="False" onclick="addAll(document.forms[0].select27,document.forms[0].select26);changeSel(document.forms[0].select27,document.forms[0].select25,'0')"/>
                                                         </div></td>
                                                     </tr>
                                                   </table>
                                                 </div></td>
                                               <td>&nbsp;</td>
                                               <td align="right"> <select id="select27"
           style="WIDTH: 180px; HEIGHT: 120px" multiple="multiple" size="4"
           name="select27">
                                                   <option value="b">b</option>
                                                   <option value="c" selected>c</option>
                                                 </select></td>
                                             </tr>
                                           </table></td>
                                         <td colspan="2" valign="top" nowrap>
                                           <table  border="0" cellspacing="0" cellpadding="0" id="table36">
                                             <tr>
                                               <td> <select id="select28"
           style="WIDTH: 180px; HEIGHT: 120px" multiple="multiple" size="4"
           name="select28">
                                                   <option value="b1">b1</option>
                                                 </select></td>
                                               <td width="50" align="middle" valign="middle" bordercolorlight="#C0C0C0" bordercolordark="#FFFFFF" class="tableblackborder"><div align="center">
                                                   <table width="80%" height="110" border="0" align="center" cellpadding="0" cellspacing="0" id="table37">
                                                     <tr>
                                                       <td><div align="center">
                                                           <input type="button"  name="button2" id="button33" style="WIDTH: 30px"  value=">" onclick="selChange(document.forms[0].select28,document.forms[0].select29);changeSel(document.forms[0].select29,document.forms[0].select30,'1')"/>
                                                         </div></td>
                                                     </tr>
                                                     <tr>
                                                       <td><div align="center">
                                                           <input name="button2" type="button" class="button" id="button34" style="WIDTH: 30px" value=">>" causesvalidation="False" onclick="addAll(document.forms[0].select28,document.forms[0].select29);changeSel(document.forms[0].select29,document.forms[0].select30,'1')"/>
                                                         </div></td>
                                                     </tr>
                                                     <tr>
                                                       <td height="5"><div align="center"></div></td>
                                                     </tr>
                                                     <tr>
                                                       <td><div align="center">
                                                           <input name="button2" type="button" class="button" id="button35" style="WIDTH: 30px" value="<" causesvalidation="False" onclick="selChange(document.forms[0].select29,document.forms[0].select28);changeSel(document.forms[0].select29,document.forms[0].select30,'1')"/>
                                                         </div></td>
                                                     </tr>
                                                     <tr>
                                                       <td><div align="center">
                                                           <input name="button2" type="button" class="button" id="button36" style="WIDTH: 30px" value="<<" causesvalidation="False" onclick="addAll(document.forms[0].select29,document.forms[0].select28);changeSel(document.forms[0].select29,document.forms[0].select30,'1')"/>
                                                         </div></td>
                                                     </tr>
                                                   </table>
                                                 </div></td>
                                               <td>&nbsp;</td>
                                               <td> <select id="select29"
           style="WIDTH: 180px; HEIGHT: 120px" multiple="multiple" size="4"
           name="select29">
                                                   <option value="b2">b2</option>
                                                 </select></td>
                                             </tr>
                                           </table></td>
                                       </tr>
                                       <tr>
                                         <td height="10" colspan="2" nowrap></td>
                                         <td width="275" valign="top">&nbsp;</td>
                                       </tr>
                                       <tr>
                                         <td nowrap><strong>Project Experience</strong></td>
                                         <td colspan="2" nowrap><strong>SubProject
                                           Experience</strong></td>
                                       </tr>
                                       <tr>
                                         <td height="10" valign="top" nowrap><strong>
                                           <select name="select30" id="select30" style="WIDTH: 179px" onchange="getResult('1',document.forms[0].select30.value);">
                                             <option value=""></option>
                                             <option value="b2">b2</option>
                                           </select>
                                           </strong></td>
                                         <td colspan="2" valign="top" nowrap><strong>
                                           <select name="select31" id="select31" style="WIDTH: 179px" onchange="getResult('2',document.forms[0].select31.value);">
                                             <option> </option>
                                             <option value="b13">b13</option>
                                             <option value="b14">b14</option>
                                           </select>
                                           </strong></td>
                                       </tr>
                                       <tr>
                                         <td height="10" valign="top" nowrap>
                                           <table  border="0" cellspacing="0" cellpadding="0" id="table38">
                                             <tr>
                                               <td> <select id="select32"
           style="WIDTH: 180px; HEIGHT: 120px" multiple="multiple" size="4"
           name="select32">
                                                   <option value="b11">b11</option>
                                                   <option value="b12">b12</option>
                                                 </select></td>
                                               <td width="50" align="middle" valign="middle" bordercolorlight="#C0C0C0" bordercolordark="#FFFFFF" class="tableblackborder"><div align="center">
                                                   <table width="80%" height="110" border="0" align="center" cellpadding="0" cellspacing="0" id="table39">
                                                     <tr>
                                                       <td><div align="center">
                                                           <input name="button2" type="button" class="button" id="button37" style="WIDTH: 30px"  value=">" onclick="selChange(document.forms[0].select32,document.forms[0].select33);changeSel(document.forms[0].select33,document.forms[0].select31,'2')"/>
                                                         </div></td>
                                                     </tr>
                                                     <tr>
                                                       <td><div align="center">
                                                           <input name="button2" type="button" class="button" id="button38" style="WIDTH: 30px" value=">>" causesvalidation="False" onclick="addAll(document.forms[0].select32,document.forms[0].select33);changeSel(document.forms[0].select33,document.forms[0].select31,'2')"/>
                                                         </div></td>
                                                     </tr>
                                                     <tr>
                                                       <td height="5"><div align="center"></div></td>
                                                     </tr>
                                                     <tr>
                                                       <td><div align="center">
                                                           <input name="button2" type="button" class="button" id="button39" style="WIDTH: 30px" value="<" causesvalidation="False" onclick="selChange(document.forms[0].select33,document.forms[0].select32);changeSel(document.forms[0].select33,document.forms[0].select31,'2')"/>
                                                         </div></td>
                                                     </tr>
                                                     <tr>
                                                       <td><div align="center">
                                                           <input name="button2" type="button" class="button" id="button40" style="WIDTH: 30px" value="<<" causesvalidation="False" onclick="addAll(document.forms[0].select33,document.forms[0].select32);changeSel(document.forms[0].select33,document.forms[0].select31,'2')"/>
                                                         </div></td>
                                                     </tr>
                                                   </table>
                                                 </div></td>
                                               <td>&nbsp;</td>
                                               <td> <select id="select33"
           style="WIDTH: 180px; HEIGHT: 120px" multiple="multiple" size="4"
           name="select33">
                                                   <option value="b13">b13</option>
                                                   <option value="b14">b14</option>
                                                 </select></td>
                                             </tr>
                                           </table></td>
                                         <td colspan="2" valign="top" nowrap>
                                           <table  border="0" cellspacing="0" cellpadding="0" id="table40">
                                             <tr>
                                               <td> <select id="select34"
           style="WIDTH: 180px; HEIGHT: 120px" multiple="multiple" size="4"
           name="select34">
                                                   <option value="b111">b111</option>
                                                   <option value="b112">b112</option>
                                                   <option value="b113">b113</option>
                                                 </select></td>
                                               <td width="50" align="middle" valign="middle" bordercolorlight="#C0C0C0" bordercolordark="#FFFFFF" class="tableblackborder"><div align="center">
                                                   <table width="80%" height="110" border="0" align="center" cellpadding="0" cellspacing="0" id="table41">
                                                     <tr>
                                                       <td><div align="center">
                                                           <input name="button2" type="button" class="button" id="button41" style="WIDTH: 30px"  value=">" onclick="selChange(document.forms[0].select34,document.forms[0].select35);"/>
                                                         </div></td>
                                                     </tr>
                                                     <tr>
                                                       <td><div align="center">
                                                           <input name="button2" type="button" class="button" id="button42" style="WIDTH: 30px" value=">>" causesvalidation="False" onclick="addAll(document.forms[0].select34,document.forms[0].select35)"/>
                                                         </div></td>
                                                     </tr>
                                                     <tr>
                                                       <td height="5"><div align="center"></div></td>
                                                     </tr>
                                                     <tr>
                                                       <td><div align="center">
                                                           <input name="button2" type="button" class="button" id="button43" style="WIDTH: 30px" value="<" causesvalidation="False" onclick="selChange(document.forms[0].select35,document.forms[0].select34);"/>
                                                         </div></td>
                                                     </tr>
                                                     <tr>
                                                       <td><div align="center">
                                                           <input name="button2" type="button" class="button" id="button44" style="WIDTH: 30px" value="<<" causesvalidation="False" onclick="addAll(document.forms[0].select35,document.forms[0].select34)"/>
                                                         </div></td>
                                                     </tr>
                                                   </table>
                                                 </div></td>
                                               <td>&nbsp;</td>
                                               <td> <select id="select35"
           style="WIDTH: 180px; HEIGHT: 120px" multiple="multiple" size="4"
           name="select35">
                                                   <option value="b114">b114</option>
                                                   <option value="b115">b115</option>
                                                   <option value="b116">b116</option>
                                                 </select></td>
                                             </tr>
                                           </table></td>
                                       </tr>
                                       
                                           </table></td>
                                       </tr>
                                     </table>
                                     </fieldset></td>
                                 </tr>
                               </table></td>
                           </tr>
                         </tbody>
                       </table></TD>
                   </TR>
                 </TBODY>
               </TABLE>
               <TABLE cellSpacing=0 cellPadding=0 width="95%" align=center border=0>
                 <TBODY>
                   <TR valign="top">
                     <TD width="97%" align="left" valign="middle"><div align="right"><input style="width:50px" class="button" type="button" value="Save"
        name="B32">&nbsp;&nbsp;<input style="width:50px" class="button" type="button" value="Undo" name="B3">
                       </div></TD>
                   </TR>
                 </TBODY>
               </TABLE>
               
             </TD>
           </TR>
         </TBODY>
    </TABLE>
       <script type="text/javascript" src="images/bottom.js"></script></TD></TR></TBODY></TABLE>
 </BODY></form></HTML>

  虽然这个JSP复杂多了,但是关于AJAX的代码还是跟前面的例子一样,在这里我也就不多说了,请大家自己看。

  六、蛇足

  好了,例子都说完了。废话也就不多说了,只说一件事:AJAX好像来势汹汹,要把我们用得极其普遍、极其习惯的Struts等的MVC开发模式取代掉一样。

  其实,AJAX有它的优点,同样也有它的缺点。它一般用在用户不想等待返回结果的时候和复杂的UI上;其他时候,我们还是可以使用Struts等常规方法。

  一句话,不要为了使用AJAX而使用AJAX,而是要在适合于使用AJAX的地方使用AJAX。
 
  注:本文参考了一些网友的作品,不好列出参考文献,在此对他们的工作表示感谢!

上一页

作者:  来源:  (责任编辑:webjx)    

推荐给好友】【打印】【关闭】    

  相关评论

您的姓名:
评论内容: