【发布时间】:2010-11-14 23:13:51
【问题描述】:
在 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'];?>");
}
在 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'];?>");
}
}
错误信息:
语法错误 for(在数组中)
谁能帮帮我..
【问题讨论】:
-
JavaScript 和 PHP 无法交互... PHP 在发送到客户端之前创建 JavaScript 代码。发送到客户端后,执行。如果您查看源代码,您会看到 PHP 输出的是
for( in Array){(因为$viewfield是一个空变量,而$viewfields是一个 PHP 数组,当表示为字符串时显示为Array。 ) 您需要全部使用 JavaScript 或全部使用 PHP,您不能混合使用这两种语言。 -
但由于在 jquery 中使用 php,我在 document.ready() 关闭标记中收到错误消息。如果我为每个 php 代码删除它,错误就消失了,并且 document.location 工作正常。那么现在如何为 JQuery 中的每个代码编写呢?