在JSON对象上迭代
大家好, 我通常是Perl程序员,我在JavaScript中有一些背景,并试图创建Googleish Selector Div. 请忍受我,请原谅长期的介绍... 这是细节: 我们的网站按位置过滤内容. 我使用邮政编码查找半径. 因此,如果用户想从他们不知道邮政编码的位置查看内容,我已经设置了这个整洁的小输入字段,使您可以键入zipcode或city名称. 一旦您开始键入城市名称,它就会为所有"启动"的城市查询一个数据库. 一切都很好,但是由于对我的CGI脚本的所有重复请求,我注意到延迟. (我正在使用onkeyup事件来触发AJAX请求.) 所以!! 我有这个Brite的想法可以在输入的第一个字母中查询一次数据库,让Perl脚本返回JSON对象并使用JavaScript进行进一步的过滤(在后续的键盘事件上) 仅提出一个AJAX请求,然后使用从该请求中返回的数据来进一步缩小结果. 如果用户回到输入字段为空的位置,则我将arbitary变量设置为"空",下一个字母进入新的Ajax请求将使用新的"第一个"字母进行. 所以我的问题... 我如何迭代返回的JSON对象? 到目前为止,这是我的代码:
- // AJAX CODE //
- // !!!!!!!!!
- var response = 'empty'; // my arbitrary var that decides weather to AJAX or not.
- // My AJAX request function...
- function makeRequest(url) {
- var httpRequest;
- if (window.XMLHttpRequest) { // Mozilla, Safari, ...
- httpRequest = new XMLHttpRequest();
- if (httpRequest.overrideMimeType) {
- httpRequest.overrideMimeType('text/xml');
- // See note below about this line
- }
- }
- else if (window.ActiveXObject) { // IE
- try {
- httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
- }
- catch (e) {
- try {
- httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
- }
- catch (e) {}
- }
- }
- if (!httpRequest) {
- alert('Giving up :( Cannot create an XMLHTTP instance');
- return false;
- }
- httpRequest.onreadystatechange = function() { response = httpRequest.responseText;
- // Here I set the global var response to the JSON object
- // and call the function that updates the div on the page. "updateAjax()"
- updateAjax();
- };
- httpRequest.open('GET', url, true);
- httpRequest.send('');
- }
- // THIS is the function that get's called each time a user types a letter...
- function assembleRequest(b) {
- // First make hidden "result" div visible...(cross browser)
- if(document.getElementById){
- document.getElementById('result').style.visibility='visible';
- }
- else if(document.all && !document.getElementById){
- document.all.result.style.visibility='visible';
- }
- // Find out if they have cleared the input field...
- if(b.value){
- if(response == 'empty'){ // If so make new AJAX request...
- makeRequest('ajax/cityAjax.cgi?city='+b.value);
- }
- // If not just filter through what we already have...
- else{
- updateAjax(b);
- }
- }
- // If they have cleared the field set flag to ensure new AJAX request on next letter entered.
- else{
- response = 'empty';
- }
- }
- // THIS is where the problem is... I need to iterate over the JSON object
- // and use regexe to see if the "city name" field matches...
- function updateAjax(b){
- var inputText = b.value;
- var responseText = response;
- for(responseText){
- // WHAT TO DO HERE????
- // I WANT to generate a: "<option value="27603">Raleigh, NC</option>"
- // kinda thing for each element in the JSON object...
- }
- document.getElementById("result").innerHTML = responseText;
- }
- // This function just updates the input field to the correct zip, for submitting the form...
- function inputCity(e){
- var it = e.cities.options[e.cities.selectedIndex].value;
- if (it != ""){
- if(document.getElementById){
- document.getElementById('zipcode').value = it;
- document.getElementById('result').style.visibility='hidden';
- document.getElementById('result').blur();
- }
- else if(document.all && !document.getElementById){
- document.searchZip.zipcode.value = it;
- document.all.result.style.visibility='hidden';
- document.all.result.blur();
- }
- }
- else{
- alert('Error completing request');
- }
- }
- // END AJAX
- //
- //
从我的perl脚本返回的JSON对象是这种格式: $ json = { 27603:罗利:NC, 25071:elkview:WV, 10001:纽约:纽约, ETC.... }; 我如何到达城市名称字段,看看它是否与用户输入的文本匹配??? 任何帮助将不胜感激!!! 在尝试使用JSON和REPONSONDS标志VAR之前,我的工作原理(尽管缓慢而不稳定). 谢谢!