我正在尝试创建一个简单的函数,该函数将在调用时返回正确的字符串:
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',data),function(index, 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>