javascript 我无法从 onchange 事件调用两个函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16030030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 02:59:08  来源:igfitidea点击:

I can't call two functions from onchange event

javascriptfunctiondom-events

提问by dscxz

I have to call an ajax function as well as call an additional function to get another value by giving the above text field as the input. I want to get the return from that function. Here is the code I have used to call the 2 functions, but only the first function ajax (this) is only working, I am getting the return after calling the ajax function whereas I am not getting any return value for the second function. What am I doing wrong? Here is my code:

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

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

My functions are:

我的职能是:

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;
}

回答by hsz

Just try with:

只需尝试:

onchange="ajax(this);locname(this);"

回答by Barmar

Don't put quotes around this. That passes the literal string instead of the value of the variable this.

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