我正在尝试创建一个简单的函数,在调用时将返回正确的字符串:
function getState(abbr){ if (abbr=="WY") { return "Wyoming"; } }
然后电话是这样的:
var stateName = getState("WY");
然而,所有返回的是:0
对不起,如果我遗漏了明显的东西.
更新 – 我的原始概率是因为“&”这是我正在处理的真实代码:
function getState(abbr){ var url = "states.asp" var state = ""; $.get(url,function(data) { var i = 0; $.each($('state',data),function(index,el) { if (abbr == ($(this).attr("abbr"))){ //alert($(this).attr("abbr")); state = $(this).text(); }//if (abbr == $(this).attr("abbr")){ });//$.each($('state',el) { }).success(function() { alert("x" + state); return state; }); //.success(function() { //$.get(url,function(data) { alert("y" + state); return state; }
我的电话结果是“未定义”:
alert(getState("WY"));
警报(“x”状态)有效.
更新#2 —
这里是states.asp生成的所有内容(现在)…稍后它将返回公司等:
<?xml version="1.0" encoding="utf-8"?> <STATELIST> <STATE abbr="AL">Alabama</STATE> <STATE abbr="AK">Alaska</STATE> <STATE abbr="AZ">Arizona</STATE> <STATE abbr="AR">Arkansas</STATE> <STATE abbr="CA">California</STATE> <STATE abbr="CO">Colorado</STATE> <STATE abbr="CT">Connecticut</STATE> <STATE abbr="DE">Delaware</STATE> <STATE abbr="FL">Florida</STATE> <STATE abbr="GA">Georgia</STATE> <STATE abbr="HI">Hawaii</STATE> <STATE abbr="ID">Idaho</STATE> <STATE abbr="IL">Illinois</STATE> <STATE abbr="IN">Indiana</STATE> <STATE abbr="IA">Iowa</STATE> <STATE abbr="KS">Kansas</STATE> <STATE abbr="KY">Kentucky</STATE> <STATE abbr="LA">Louisiana</STATE> <STATE abbr="ME">Maine</STATE> <STATE abbr="MD">Maryland</STATE> <STATE abbr="MA">Massachusetts</STATE> <STATE abbr="MI">Michigan</STATE> <STATE abbr="MN">Minnesota</STATE> <STATE abbr="MS">Mississippi</STATE> <STATE abbr="MO">Missouri</STATE> <STATE abbr="MT">Montana</STATE> <STATE abbr="NE">Nebraska</STATE> <STATE abbr="NV">Nevada</STATE> <STATE abbr="NH">New Hampshire</STATE> <STATE abbr="NJ">New Jersey</STATE> <STATE abbr="NM">New Mexico</STATE> <STATE abbr="NY">New York</STATE> <STATE abbr="NC">North Carolina</STATE> <STATE abbr="ND">North Dakota</STATE> <STATE abbr="OH">Ohio</STATE> <STATE abbr="OK">Oklahoma</STATE> <STATE abbr="OR">Oregon</STATE> <STATE abbr="PA">Pennsylvania</STATE> <STATE abbr="RI">Rhode Island</STATE> <STATE abbr="SC">South Carolina</STATE> <STATE abbr="SD">South Dakota</STATE> <STATE abbr="TN">Tennessee</STATE> <STATE abbr="TX">Texas</STATE> <STATE abbr="UT">Utah</STATE> <STATE abbr="VT">Vermont</STATE> <STATE abbr="VA">Virginia</STATE> <STATE abbr="WA">Washington</STATE> <STATE abbr="WV">West Virginia</STATE> <STATE abbr="WI">Wisconsin</STATE> <STATE abbr="WY">Wyoming</STATE> </STATELIST>
解决方法
最简单的方法是制作哈希 – 不需要任何功能.
var states = { 'AL': 'Alabama','AK': 'Alaska','AZ': 'Arizona','AR': 'Arkansas','CA': 'California','CO': 'Colorado','CT': 'Connecticut',... 'WY': 'Wyoming' }; var stateName = states["WY"];
编辑
现在我更好地理解getState()需要从服务器检索状态名称.这使您进入异步编码的世界,这与正常的同步编码完全不同.
要实现的最重要的事情是getState()不能简单地返回给定状态缩写的状态名称.为什么?因为对服务器的ajax调用是异步的 – 换句话说,getState()不会在返回之前等待服务器的响应.
基本上有两种处理异步性的方法:
>将回调函数传递给getState(),告诉它在收到响应时该怎么做
>安排getState()返回一个特殊类型的对象,称为“promise”,可以在调用getState()的地方处理,以便在服务器响应时它会响应.
var states = {};//cache of state names,with state abbreviations as keys function getState(abbr) { var dfrd = $.Deferred();//A deferred object,whose promise will be returned. if(!states[abbr]) { $.ajax({ url: "states.asp",dataType: 'XML',success: function(data) { //Load up the cache $.each($('state',function(i,el) { states[el.attr('abbr')] = $(el).text(); }); //Now resolve or reject the deferred object depending in whether states[abbr] has been cached if(states[abbr]) { dfrd.resolve(abbr,states[abbr]);//Success! Let's resolve the deferred object (and its promise). } else { dfrd.reject(abbr,'States successfully downloaded but ' + abbr + ' was not included'); } },error: function() { dfrd.reject(abbr,'Download of states Failed'); } }); } else { //The state name is already cached //The deferred object (and its promise) can be resolved without needing to make another ajax call. dfrd.resolve(abbr,states[abbr]); } return dfrd.promise(); }
未经测试
现在您需要知道的是如何调用getState().
getState("WY").done(function(abbr,state) { alert(abbr + ': ' + state); //other stuff here }).fail(function(abbr,message) { alert(abbr + ': ' + message); //other stuff here });
如您所见,您希望getState()返回的值现在显示为.done()函数的第二个参数.为了更好地衡量,缩写(“WY”)显示为第一个参数.
如果你想处理错误条件(总是一个好主意),那么在.fail()回调中这样做.