Appcelerator:简单的邮政编码城市查找器
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Appcelerator:简单的邮政编码城市查找器相关的知识,希望对你有一定的参考价值。
Appcelerator Titanium Mobile is a framework + IDE designed to help you build iPhone applications using only html and javascript. This example shows how to make a simple Ajax request
<html> <head> <title>Find City By ZIP Code</title> <script type="text/javascript" src="jquery-1.3.2.js"></script> <style type="text/css" media="screen"> #textField{ width:100px; height:30px } #search-btn{ width:100px; height:47px; color:#0000ff; } #response-txt{ width:300px; text-align:center; font-family:helvetica, impact, sans-serif; font-weight: bold; } .center { margin-right:auto;margin-left:auto;width:320px; } </style> <script type="text/javascript" charset="utf-8"> var xhr; var xhrResponse; var alert; var xmlCity; var xmlState; var zipCode; var tf; var searchBtn; function initialize(e){ /**************************** Create Ajax Request ****************************/ xhr = Titanium.Network.createHTTPClient(); xhrResponse = $("#response-txt"); /**************************** Create TextField ****************************/ tf = Titanium.UI.createTextField({ id:'textField', value:'90026', color:'#336699', backgroundColor:'#eeeeee', returnKeyType:Titanium.UI.RETURNKEY_DONE, enableReturnKey:true, keyboardType:Titanium.UI.KEYBOARD, autocorrect:false, hintText:'Enter ZIP', textAlign:'left', clearOnEdit:true, borderStyle:Titanium.UI.INPUT_BORDERSTYLE_BEZEL, clearButtonMode:Titanium.UI.INPUT_BUTTONMODE_NEVER, }); $(tf).bind("change", onTfChangeHandler ); function onTfChangeHandler(e){ tf.value = e.value; } /**************************** Create Search Button ****************************/ searchBtn = Titanium.UI.createButton({ id:'search-btn', title:'Search' }); $(searchBtn).bind("click", onSearchClickHandler ); function onSearchClickHandler(e){ constructAndSend(); } /**************************** Ajax onLoad Request ****************************/ xhr.onload = function() { Titanium.API.info( "xhr.onload: " ); var xml = this.responseXML; // xml now is a Javascript DOM object you can use var xmlDoc = xml.documentElement; try { // get a value of the first tag myelement found in XML doc xmlCity = xmlDoc.getElementsByTagName("CITY")[0].childNodes[0].nodeValue.toString(); xmlState = xmlDoc.getElementsByTagName("STATE")[0].childNodes[0].nodeValue.toString(); } catch(err) { $(xhrResponse).html = ""; constructAlert( "Zip Code Not Found. "); } //xhrResponse.innerHTML = zipCode + "<br>is the ZIP Code for<br>" + xmlCity + ", " + xmlState; $(xhrResponse).html( zipCode + "<br>is the ZIP Code for<br>" + xmlCity + ", " + xmlState ); }; } function constructAndSend(){ //Capture the Zip Code zipCode = $(tf).val(); Titanium.API.info( "constructAndSend: " + zipCode ); //Confirm that a proper Zip Code has been entered if ( zipCode != null && zipCode.length == 5 ){ //Fire Off the Ajax Request xhr.open("GET","http://www.webservicex.net/uszip.asmx/GetInfoByZIP?USZip=" + zipCode); xhr.send(null); } else { //Show any errors constructAlert( "A 5-digit ZIP Code must be entered." ); } } /**************************** Create Alert Message ****************************/ function constructAlert( message ){ var alert = Titanium.UI.createAlertDialog(); alert.setTitle("Error"); alert.setMessage( message ); alert.show(); return; } </script> <script type="text/javascript" charset="utf-8"> $(document).ready(function() { initialize(); }); </script> <body style="background-color:#999999"> <div id="textField" class="center"></div> <br/> <div id="search-btn" class="center"></div> <br/><br/> <div id="response-txt" class="center"></div> </body> </html>
以上是关于Appcelerator:简单的邮政编码城市查找器的主要内容,如果未能解决你的问题,请参考以下文章
Mongodb:通过最高的邮政编码城市人口查找具有最高数量的邮政编码和订单状态的城市