根据 req.responseText 选择一个选项
我不知道是我想太多还是什么。我只是希望能够根据通话结果选择特定选项。目前调用根据邮政编码生成城市、州、国家/地区。 GET 的响应是“萨克拉门托 | 加利福尼亚州 | 美国” 我可以轻松地将响应放入输入框中,但我不知道如何根据响应选择选项。这可能吗?我一直在查看一些方法属性,但我并没有真正看到任何可以使用的东西。
这是获取脚本。
<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>
选项设置<选项名称=“(缩写)”值=“(全名)”>全名
即
我只是在寻找替换 .value 属性的东西。类似 document.getElementById('state').childNode.attribute.name = Result[1]
之类的东西。
这是完整页面文件的链接 http://ge.tt/99dJ1J9?c
I don't know if I am just thinking too hard about this or what. I just want to be able to select a specific option based on the result I get from a call. Currently call produces City, State, Country based on Zip code. The response from the GET is "Sacramento | CA | United States" I can easily put the responses into input boxes, but I can't figure out how to select an option based on the response. Is this possible? I've been looking through some of the method properties and i'm not really seeing anything that I can use.
Here is the Get script.
<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>
Here is the Response 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 setup<option name="(abbr.)" value="(full name)">Full Name</option>
i.e.<option name="CA" value="California">California</option>
I'm just looking for something to replace the .value property. Something like document.getElementById('state').childNode.attribute.name = Result[1]
or something.
Here is a link to the full page file http://ge.tt/99dJ1J9?c
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是您没有选项值中状态的缩写,并且响应包含缩写。
另外,我会向您推荐一些 javascript 框架。它将对您完成 Ajax、表单验证等常见任务有很大帮助。
只需谷歌 javascript 框架即可找到最适合您需求的一个:-)
The problem is that you do not have the abbrev for the state in option value and the response contains the abbrev.
Also, i would recommend you some javascript framework. It would help you very much with common taks like Ajax, form validation and so on.
Just google javascript framework and find one that would fit your needs best :-)
使用它来设置 SELECT 对象中的值(以下代码适用于城市):
Use this to set the value in your SELECT object (below code is for city):