简体   繁体   English

在函数内调用javascript函数

[英]call javascript function within function

Javascript Nub. Javascript Nub。

Running a function that checks the state of 5 selectboxes, if their value is "Select" i want to run a function to populate them based on another value. 运行一个检查5个选择框状态的函数,如果它们的值为“ Select”,我想运行一个函数以基于另一个值填充它们。 I can do this within the first function but it gets very long. 我可以在第一个函数中执行此操作,但是它会很长。

abbreviated version. 缩写版本。

function chek{

var tligc = document.getElementById('assigc').innerHTML;   
var tligd = document.getElementById('assigd').innerHTML;



 if(tligc == 'Select'){document.getElementById('otherAgency3').className = 'textBox';
    return setass(3);
       }      

if(tligd == 'Select'){document.getElementById('otherAgency4').className = 'textBox'
 return setass(4);
}

Etc, 等等,

function setass(value){  

    var KaRa = document.getElementById('assig').innerHTML;

                           if(KaRa =='Planning Officer'){
 document.getElementById('otherAgency'+[value]).options.length=0
 document.getElementById('otherAgency'+[value]).options[0]=new Option("Select", "Select", true, false)  
 document.getElementById('otherAgency'+[value]).options[1]=new Option("Situation Unit", "Situation Unit", true, false)
 document.getElementById('otherAgency'+[value]).options[2]=new Option("Management Support", "Management Support", true, false)  
         return  }
else if(KaRa =='Operations Officer'){
 document.getElementById('otherAgency'+[value]).options.length=0
 document.getElementById('otherAgency'+[value]).options[0]=new Option("Select", "Select", true, false)  
 document.getElementById('otherAgency'+[value]).options[1]=new Option("Staging Area Manager", "Staging Area Manager", true, false)
    return }
else if(KaRa =='Logistics Officer'){
 document.getElementById('otherAgency'+[value]).options.length=0
 document.getElementById('otherAgency'+[value]).options[0]=new Option("Select", "Select", true, false)  
 document.getElementById('otherAgency'+[value]).options[1]=new Option("Supply Unit", "Supply Unit", true, false)
 document.getElementById('otherAgency'+[value]).options[2]=new Option("Communications Support", "Communications Support", true, false)  
 document.getElementById('otherAgency'+[value]).options[3]=new Option("Facilities Unit", "Facilities Unit", true, false)
        return }
    else{document.getElementById('otherAgency'+[value]).options.length=0;
      return }
       }
 }    

Its populating the first option but not running the rest of the checking function for the remainer if tests, any pointers on how to run the function setass and then return to the main function again at the right point? 它填充了第一个选项,但没有运行其余的if函数(如果进行测试)检查功能,是否有任何指针说明如何运行函数setass,然后在正确的位置再次返回主函数? Im assuming 'return' is not the correct command (if im WAY off just say and ill do it the long way). 我以为'return'不是正确的命令(如果我要走了,那就长话大说吧)。 Thankyou 谢谢

The remainder of the if statements will not be called because you are returning from the function if the first one is hit.: if语句的其余部分将不会被调用,因为如果第一个命中,您将从函数返回。

if(tligc == 'Select') {
    document.getElementById('otherAgency3').className = 'textBox';
    return setass(3);
}  

The return function in javascript, will return immediately to the calling function, either with a value or without one. javascript中的return函数将立即返回调用函数,带值或不带值。 The code above is basically saying: "If you get to this point, run the setass function with a parameter of 3, and returns the result back to the caller of chek . 上面的代码基本上是说:“如果你到这一点,运行setass以3参数的函数,并返回结果返回到调用者chek

You need to remove this return statement if you want the method to continue without ending. 如果希望方法继续执行而不结束,则需要删除此return语句。

if(tligc == 'Select') {
    document.getElementById('otherAgency3').className = 'textBox';
    setass(3);
}  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM