采用一个开源的AJAX库:Ajax Toybox 描述: 在注册或购物车结帐的时候,需要用户填入个人资料,这个环节可以简练一下,我们只需客人填入邮政编码,然后就根据这个邮政编码,自动从数据库中取出相应的省,市等地址信息。这样既可以减少客户的输入,增加客户体验,又可以减少由于资料输入而产生的错误。 实现: HTML <script> function createRequestObject() { var ro; var browser = navigator.appName; if(browser == "Microsoft Internet Explorer"){ ro = new ActiveXObject("Microsoft.XMLHTTP"); }else{ ro = new XMLHttpRequest(); } return ro; }
var http = createRequestObject();
function sndReq(zip) { http.open('get', 'zipcode.php?zip='+zip); http.onreadystatechange = handleResponse; http.send(null); }
function handleResponse() { if(http.readyState == 4){ var response = http.responseText; var update = new Array();
if(response.indexOf('|' != -1)) { update = response.split('|'); document.getElementById("city").value = update[0]; document.getElementById("state").value = update[1]; } } } </script> <h3>Enter A United States Zipcode, Then Tab</h3> <table align="center"> <tr> <td>Enter Zipcode:</td> <td><input type="text" id="zipcode" name="zipcode" onBlur="sndReq(this.value);"/></td> </tr> <tr> <td>City:</td> <td><input type="text" id="city" name="city"/></td> </tr> <tr> <td>State:</td> <td><input type="text" id="state" name="state"/></td> </tr> </table>
|