foreach相当于在jQuery中的PHP? [英] foreach equivalent of php in jquery?

查看:123
本文介绍了foreach相当于在jQuery中的PHP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PHP中是否有一个在JQuery中的foreach代码?
我在php中有一个代码,就像

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


$ b $ if(<?= $ viewfield ['Attribute'] ['type'];?> =='text'||<?= $视场[ '属性'] [ '类型'];?> == '日期' ||< = $视场[ '属性'] [ '类型'];?> == '编号'){
$(< input id = input< = $ viewfield ['Attribute'] ['sequence_no'];?> type ='text'style ='width:<?= $ viewfield [属性'] ['size'];?> px'data-attr =<?= $ viewfield ['Attribute'] ['type'];?>>< / input>< br> ).appendTo(#fb_contentarea_col1down21#<?= $ viewfield ['Attribute'] ['sequence_no'];?>);


$ b其他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# < = $视场[ '属性'] [ 'sequence_no'];?>中);





$ b

在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 [属性'] ['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'];?>);

$ b $ else if(<?= $ viewfield ['Attribute'] ['type'];?> =='textarea'){
$( < textarea style ='width:<?= $ viewfield ['Attribute'] ['size'];?> px'data-attr =<?= $ viewfield ['Attribute'] ['type'] ;< / textarea>< br>)。appendTo(#fb_contentarea_col1down21#<?= $视场[ '属性'] [ 'sequence_no'];?>中);

code










$ b $ p


$ b

错误消息:

语法错误
(在数组中)

有人可以帮我吗?

解决方案

$。每个函数都是类似的。



它允许您使用回调函数遍历每个项目的数组:

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

$ b $ .each(arr,function(index,value){
//使用值
});

如果你想打破这个循环,你可以用 return false; 或者如果您只想跳过一个迭代(继续),您返回true;


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'];?>");
}

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

EDIT 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'];?>");
 }

}

Error message:

syntax error for( in Array)

Can someone help me..

解决方案

The $.each function is similar.

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
});

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;

这篇关于foreach相当于在jQuery中的PHP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆