簡體   English   中英

從javascript中的函數返回時遇到問題

[英]Trouble in returning from a function in javascript

我在while循環中使用javascript函數時遇到麻煩,我的函數只能執行一次,下面是代碼。

我在while循環中調用了一個函數,該調用的函數調用了另一個函數。 我想執行第一個函數直到while循環結束,但是它只執行一次。

提前致謝。

while(p<=10)
    {
        k=0;
        l=0;
        var contExp= mycontents[p].split("#");
        var divExp= mydivs[p].split(",");
        var schtime=contExp[k];
            alert(contExp[k]);
        document.getElementById('gymlocationz').value=contExp[k+1];
        document.getElementById('fitness').value=contExp[k+2];
        document.getElementById('duration').value="1 hour";
        alert(p);
        return select_visibility(divExp[l],divExp[l+1],divExp[l+2],contExp[k],mycontents[p]);
        //l=l+3;
        p++;
    }

function select_visibility(str,str1,timeid,time,cont) 
{
  var contExp= cont.split("#");
  var e = document.getElementById(str);
  var esub=document.getElementById(str+'sub');
  var fulldv=str+'sub';
  var result = timeid.match(/([0-9]+)/g);
    $('#'+str1).addClass('act');
    $('#'+str+'sub').addClass('act');
    document.getElementById(timeid).value=time;
    document.getElementById('fitness'+result).value=document.getElementById('fitness').value;
    document.getElementById('gymlocat'+result).value=document.getElementById('gymlocationz').value;
    document.getElementById('selectdrpact'+result).value=contExp[3];
    document.getElementById('repeat'+result).value=contExp[4];
    var s=document.getElementById('fitness'+result).value;
$("#"+str).css(
{
   "background-color":"#95c5ec",
   "border":"1px solid #ddd",
   "text-decoration":"none",
   "padding":"10px 0px",
   "margin-left":"30px"
});
    $("#"+fulldv).css(
    {
       "background-color":"#95c5ec",
       "border":"1px solid #ddd",
       "text-decoration":"none",
       "padding":"10px 0px",
       "margin-left":"41px"
    });
    e.style.display = 'block';
    esub.style.display = 'block'; 
    selecteditems();
    //return true;
}

function selecteditems()
{
    var i=1;
    var fld = "";
    document.getElementById("showselecteditems").innerHTML="";
    while(i<=53)
    {
    fldOne = document.getElementById('datepicker_value').value;
    fld = document.getElementById('timedrpact'+i).value;
    fidpartnum = document.getElementById('selectdrpact'+i).value;
    fidrepeat = document.getElementById('repeat'+i).value;

    fit=document.getElementById('fitness'+i).value;
    if(fit=="Select")
    {
        fit="Fitness Not Selected";
        }
    if(fld!="")
    {
            //var par='ddwnx'+i;

            //alert(fit+","+i+","+fld);
            var ele = document.createElement("div");
        ele.setAttribute("id","showselecteditems"+i);
        ele.setAttribute("class","inner");
        ele.innerHTML=fit+"&nbsp;,&nbsp;"+fldOne+"&nbsp;,&nbsp;"+fld+"&nbsp;,&nbsp;"+fidpartnum+"&nbsp;Paticipants, &nbsp;"+fidrepeat+"&nbsp;Repeat";
    }
    i++;
}

}

您正在while循環內使用return語句。 return語句將導致函數停止運行,並向稱為該函數的任何代碼返回值。 問題是這一行:

return select_visibility(divExp[l],divExp[l+1],divExp[l+2],contExp[k],
    mycontents[p]);

更改為此:

select_visibility(divExp[l],divExp[l+1],divExp[l+2],contExp[k],mycontents[p]);

這將調用select_visibility()函數,而不會導致包含循環的函數終止。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM