foreach相当于jquery中的php?

Ang*_*ine 14 php foreach jquery

在PHP中是否存在JQuery中的foreach代码?我在php中有一个代码,比如

<?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'];?>");
}
Run Code Online (Sandbox Code Playgroud)

在Jquery中有没有相当于foreach的东西?如何在jQuery中实现相同的功能?

编辑1:

我认为它有效,但我得到一个错误.代码和错误消息如下.

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'];?>");
 }
Run Code Online (Sandbox Code Playgroud)

}

错误信息:

(在数组中)的语法错误

有人能帮我吗..

CMS*_*CMS 43

$.每个功能类似.

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

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


$.each(arr, function(index, value) {
  // work with value
});
Run Code Online (Sandbox Code Playgroud)

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


Bli*_*ixt 26

如果你想迭代一个对象,我会推荐JavaScript变种:

for (var key in obj) {
    alert(key + ': ' + obj[key]);
}
Run Code Online (Sandbox Code Playgroud)

也可以像这样在jQuery中迭代对象:
注意!除非您认为这种语法更易于维护,否则这样做是毫无意义的.以下语法比上面的标准JavaScript for循环具有更多的开销.

$.each(obj, function (key, value) {
    alert(key + ': ' + value);
});
Run Code Online (Sandbox Code Playgroud)

要迭代数组,这是你在标准JavaScript中的方式(假设arr是数组):

for (var i = 0, l = arr.length; i < l; i++) {
    alert(i + ': ' + arr[i]);
}
Run Code Online (Sandbox Code Playgroud)

要在jQuery中执行此操作,您可以这样做:

$.each(arr, function (index, value) {
    alert(index + ': ' + value);
});
Run Code Online (Sandbox Code Playgroud)


小智 5

Javascript支持for(data in data_array)语法。jQuery还有一个$ .each函数(如前所述)

  • for-in遍历对象的属性,而不是数组的元素。 (2认同)