0

我不知道我是否只是在想这个或什么。我只是希望能够根据我从通话中得到的结果选择一个特定的选项。当前调用根据邮政编码生成城市、州、国家。GET 的响应是“Sacramento | CA | United States” 我可以轻松地将响应放入输入框中,但我不知道如何根据响应选择选项。这可能吗?我一直在查看一些方法属性,但我并没有真正看到我可以使用的任何东西。

这是获取脚本。

<script type="text/javascript">
var req; 
var oldData; 
var doesNotSupport = true; 

function getAddress(url, number) 
{ 
if (number == "" || oldData == number || !doesNotSupport) 
    return; 

oldData = number; 
document.getElementById('city').value = "Searching ...";
document.getElementById('state').value = "Searching ...";
document.getElementById('country').value = "Searching ...";

if (window.XMLHttpRequest) { 
    req = new XMLHttpRequest; 
} else if (window.ActiveXObject) { 
    req = new ActiveXObject("Microsoft.XMLHTTP"); 
} 

if (req) { 
   req.onreadystatechange = processReqChange; 
   req.open("GET", url + '?number=' + number + '&zip=' + number, true);
   req.send(null); 
   } else { 
   alert("Your browser does not support XMLHttpRequest technology!"); 
   doesNotSupport = false; 
  } 
}
</script>

这是响应脚本

<script type="text/javascript">
function processReqChange() { 
// only if req shows "loaded" 
if (req.readyState == 4) { 
// only if "OK" 
if (req.status == 200) { 
    var Result = req.responseText.split("|");
document.getElementById('city').value = Result[0];
document.getElementById('state').value = Result[1];
         This is the problem child.
   document.getElementById('country').value = Result[2];
} else { 
    alert("There was a problem retrieving the XML data:\n" + req.statusText); 
    } 
  } 
}
</script>

选项设置
<option name="(abbr.)" value="(full name)">Full Name</option>

<option name="CA" value="California">California</option>

我只是在寻找替换 .value 属性的东西。什么东西之类document.getElementById('state').childNode.attribute.name = Result[1]的。
这是完整页面文件的链接http://ge.tt/99dJ1J9?c

4

2 回答 2

0

使用它来设置 SELECT 对象中的值(下面的代码是针对城市的):

var selObject = document.getElementById('city_select_id');     
selObject.value = document.getElementById('city').value; //value received in the response
于 2011-10-28T17:58:11.817 回答
0

问题是您没有选项值中状态的缩写,并且响应包含缩写。

另外,我会向您推荐一些 javascript 框架。它将在 Ajax、表单验证等常见任务方面为您提供很大帮助。

只需 google javascript 框架并找到最适合您需求的框架 :-)

于 2011-10-28T20:26:47.680 回答