foreach 相当于 jquery 中的 php?

2021-12-26 00:00:00 foreach jquery php

JQuery 中有没有像 PHP 一样的 foreach 代码?我在 php 中有一个代码,比如

Is there a foreach code in JQuery as in PHP? I have a code in php,like

<?php foreach ($viewfields as $viewfield): ?>       
if("<?php echo $viewfield['Attribute']['required'];?>"=='true'){
       $("<span class='req'><em> * </em></span>").appendTo("#fb_contentarea_col1down21 #label<?php echo $viewfield['Attribute']['sequence_no']?>");
   }


   if(<?=$viewfield['Attribute']['type'];?>=='text'||<?=$viewfield['Attribute']['type'];?>=='date'||<?=$viewfield['Attribute']['type'];?>=='number'){ 
       $("<input id=input<?=$viewfield['Attribute']['sequence_no'];?> type= 'text' style= 'width:<?=$viewfield['Attribute']['size'];?>px' data-attr=<?=$viewfield['Attribute']['type'];?> ></input><br>").appendTo("#fb_contentarea_col1down21 #<?=$viewfield['Attribute']['sequence_no'];?>");
   }


   else if(<?=$viewfield['Attribute']['type'];?>=='textarea'){
       $("<textarea style= 'width:<?=$viewfield['Attribute']['size'];?>px' data-attr=<?=$viewfield['Attribute']['type'];?> id=input<?=$viewfield['Attribute']['sequence_no'];?>></textarea><br>").appendTo("#fb_contentarea_col1down21 #<?=$viewfield['Attribute']['sequence_no'];?>");
}

在 Jquery 中是否有任何等效的 foreach?如何在 jQuery 中实现同样的功能?

Is there any equivalent of foreach in Jquery? How can I accomplish this same functioality in jQuery?

编辑 1:

我认为它有效,但出现错误.代码和错误信息如下.

I thought it worked but I get an error. The code and the error message is given below.

for(<?=$viewfield;?> in <?=$viewfields;?>){

 if("<?=$viewfield['Attribute']['required'];?>"=='true'){
            $("<span class='req'><em> * </em></span>").appendTo("#fb_contentarea_col1down21 #label<?php echo $viewfield['Attribute']['sequence_no']?>");
 }

 if(<?=$viewfield['Attribute']['type'];?>=='text'||<?=$viewfield['Attribute']['type'];?>=='date'||<?=$viewfield['Attribute']['type'];?>=='number'){ 
            $("<input id=input<?=$viewfield['Attribute']['sequence_no'];?> type= 'text' style= 'width:<?=$viewfield['Attribute']['size'];?>px' data-attr=<?=$viewfield['Attribute']['type'];?> ></input><br>").appendTo("#fb_contentarea_col1down21 #<?=$viewfield['Attribute']['sequence_no'];?>");
 }

 else if(<?=$viewfield['Attribute']['type'];?>=='textarea'){
            $("<textarea style= 'width:<?=$viewfield['Attribute']['size'];?>px' data-attr=<?=$viewfield['Attribute']['type'];?> id=input<?=$viewfield['Attribute']['sequence_no'];?>></textarea><br>").appendTo("#fb_contentarea_col1down21 #<?=$viewfield['Attribute']['sequence_no'];?>");
 }

}

错误信息:

语法错误for(在数组中)

syntax error for( in Array)

谁能帮我..

推荐答案

$.each 功能类似.

它允许您使用可以访问每个项目的回调函数迭代数组:

It allows you to iterate arrays using a callback function where you have access to each item:

var arr = [ "one", "two", "three", "four", "five" ];


$.each(arr, function(index, value) {
  // work with value
});

知道也许有用,如果你想打破循环,你可以用 return false; 或者如果你只想跳过一次迭代(继续),你 return真;

Maybe is useful to know, if you want to break the loop, you can do it with return false; or if you want to skip only one iteration (continue), you return true;

相关文章