foreach 相当于 jquery 中的 php?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1198436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 01:29:19  来源:igfitidea点击:

foreach equivalent of php in jquery?

phpjqueryforeach

提问by Angeline

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

JQuery 中有没有像 PHP 一样的 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'];?>");
}

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

在 Jquery 中是否有任何等效的 foreach?我怎样才能在 jQuery 中完成同样的功能?

EDIT 1:

编辑 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)

语法错误 for( in Array)

Can someone help me..

有人能帮我吗..

回答by CMS

The $.eachfunction 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;

知道可能很有用,如果你想打破循环,你可以这样做,return false;或者如果你只想跳过一次迭代(继续),你return true;

回答by Blixt

If you want to iterate an object, I would recommend the JavaScript variant:

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

for (var key in obj) {
    alert(key + ': ' + obj[key]);
}

You canalso iterate objects in jQuery like this:
Note!Doing this is pretty pointless unless you think this syntax is much simpler to maintain. The below syntax has much more overhead than the above, standard JavaScript, for-loop.

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

$.each(obj, function (key, value) {
    alert(key + ': ' + value);
});


To iterate arrays, this is how you do it in standard JavaScript (assuming arris the array):

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

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

To do it in jQuery, you can do it like this:

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

$.each(arr, function (index, value) {
    alert(index + ': ' + value);
});

回答by Torandi

Javascript supports the for(data in data_array)syntax. jQuery also has a $.each function (as already mentioned)

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

回答by Chetan Sastry

There is jQuery.each.

jQuery.each

回答by meder omuraliev

Jquery operating on selectors:

Jquery 操作选择器:

$('a').each(function() {
    $(this).click(function(e) {
       e.preventDefault()
       var href = this.href;
       open(href);
    });
    // operate on the anchor node.
});

jQuery direct $.each:

jQuery 直接 $.each:

var a = ['one', 'two'];

$.each(a, function() {
    alert(this)
});

JS: Vanilla for loop

JS:香草 for 循环

 for ( var i = 0, len = 10; i<l; ++i ) {
    alert(i)
 }

JS #2: vanilla for

JS #2:香草

 var humanLimbs = ['arms', 'legs'];
 for ( var limb in humanLimbs ) {
     if ( humanLimbs.hasOwnProperty(limb) ) {
        alert( limb )
     }
 }

Js #3: infinite loop

Js #3:无限循环

for (;;) { alert(1) } // dont try this :p