gpt4 book ai didi

javascript - 从 javascript 中的函数返回时出现问题

转载 作者:行者123 更新时间:2023-11-28 20:12:45 25 4
gpt4 key购买 nike

我在 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() 函数,而不会导致包含循环的函数终止。

关于javascript - 从 javascript 中的函数返回时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19659508/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com