根据 req.responseText 选择一个选项

发布于 2024-12-12 05:05:11 字数 1971 浏览 0 评论 0原文

我不知道是我想太多还是什么。我只是希望能够根据通话结果选择特定选项。目前调用根据邮政编码生成城市、州、国家/地区。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

智商已欠费 2024-12-19 05:05:11

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

另外,我会向您推荐一些 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 :-)

顾铮苏瑾 2024-12-19 05:05:11

使用它来设置 SELECT 对象中的值(以下代码适用于城市):

var selObject = document.getElementById('city_select_id');     
selObject.value = document.getElementById('city').value; //value received in the response

Use this to set the value in your SELECT object (below code is for city):

var selObject = document.getElementById('city_select_id');     
selObject.value = document.getElementById('city').value; //value received in the response
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文