7

我必须通过将上述文本字段作为输入来调用 ajax 函数以及调用附加函数来获取另一个值。我想从该函数中获得回报。这是我用来调用这两个函数的代码,但只有第一个函数 ajax (this) 才有效,调用 ajax 函数后我得到了返回,而第二个函数没有得到任何返回值。我究竟做错了什么?这是我的代码:

<select style="width: 305px;" name="category_id1" id="category_id1" onchange="(function(){ajax('this');return locname('this');})(this)">

我的职能是:

function ajax(control)
{
var loc=document.getElementById('category_id1').value;
//alert(loc);
var req;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
   req=new XMLHttpRequest();
}
else
{// code for IE6, IE5
   req=new ActiveXObject("Microsoft.XMLHTTP");
}
req.open("POST", "ajax.php?&loc="+loc+"", true);
req.send();

req.onreadystatechange=function(){
   if(req.readyState==4&&req.status==200){
       //$(".error").hide();
       result=req.responseText
       alert(result);
       var items = JSON.parse(result);
       //alert(items.length)
       var html = '<option value="">Select</option>';
       for ( var i = 0; i < items.length; i++ ) {
       html += "<option value='" + items[ i ] + "'>" + items[ i ] + "</option>"
     }
     document.getElementById("Ultra").innerHTML = html;
   }
   }
   }
 function locname(control)
 {
 var locName=control.value;
 //alert(locName)
 var json = <?php echo $response ?>;
 var orgName=document.getElementById("category_id").value;
//alert(json);
var html="<option>Select</option>";
var locations;
//alert(json.length);
for(var i=0; i<json.length; i++)
    {
        var item = json[i].name;
        //alert(item);
        if(orgName===item){
            //alert(item)
            locations=json[i].location;
            for(var j=0;j<locations.length;j++){
                var location=locations[j].name;
                //alert(locName)
       if(locName===location)
           {
               //alert(locName);
               var buildings=locations[j].building
               //alert(buildings)
               for(var k=0;k<buildings.length;k++)
                   {
                       var building=buildings[k];
                       //alert(building);
                html=html+"<option value='"+building+"'>"+building+"</option>";
                   }
           }

            }

}
}
document.getElementById("category_id2").innerHTML=html;
}
4

2 回答 2

20

只需尝试:

onchange="ajax(this);locname(this);"
于 2013-04-16T06:20:15.423 回答
0

不要在周围加上引号this。这会传递文字字符串而不是变量的值this

于 2013-04-16T06:20:45.517 回答