問(wèn)題描述
JQuery 中有沒有像 PHP 一樣的 foreach 代碼?我在 php 中有一個(gè)代碼,比如
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 中實(shí)現(xiàn)同樣的功能?
Is there any equivalent of foreach in Jquery? How can I accomplish this same functioality in jQuery?
編輯 1:
我認(rèn)為它有效,但出現(xiàn)錯(cuò)誤.代碼和錯(cuò)誤信息如下.
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'];?>");
}
}
錯(cuò)誤信息:
語(yǔ)法錯(cuò)誤for(在數(shù)組中)
syntax error for( in Array)
誰(shuí)能幫我..
推薦答案
$.each 功能類似.
它允許您使用可以訪問(wèn)每個(gè)項(xiàng)目的回調(diào)函數(shù)迭代數(shù)組:
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
});
知道也許有用,如果你想打破循環(huán),你可以用 return false;
或者如果你只想跳過(guò)一次迭代(繼續(xù)),你 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;
這篇關(guān)于foreach 相當(dāng)于 jquery 中的 php?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!