jQuery在foreach循环内 [英] Jquery inside foreach loop
本文介绍了jQuery在foreach循环内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在foreach循环中有此jquery代码.基本上,变量$ perf在每个循环中都会获得一个新值.如何使用jquery在每个循环中显示不同的$ perf值?有可能吗?
I have this jquery code in a foreach loop. Basicaly the variable $perf gets a new value with every loop. How can I use jquery to display the different $perf value with each loop? Is it possible?
foreach ($perfs as $perf):
<script type="text/javascript">
$(document).ready(function(){
var performerName = $(".transparency").data('title');
var divcontent = $(".transparency").html();
if ( divcontent == ' ' ){
$(".transparency").html(''+performerName+'');
}
});
</script>
<div class="transparency" data-title="<? echo $perf; ?>"> </div>
endforeach;
推荐答案
您应该这样做:
<?
foreach ($perfs as $perf):
?>
<script type="text/javascript">
$(document).ready(function(){
var $perf = "<? echo $perf; ?>"; //Get from php
alert($perf); //Show it
//Here goes the rest of your script
var performerName = $(".transparency").data('title');
var divcontent = $(".transparency").html();
if ( divcontent == ' ' ){
$(".transparency").html(performerName);
}
});
</script>
<div class="transparency" data-title="<? echo $perf; ?>"> </div>
<?
endforeach;
?>
就是这样.可以.
(我试图至少尽可能地修改您的代码,因为我不知道是否可以删除部件)
(I tried to modify your code at least as possible, cause I don't know if I can remove parts)
PS:会有更多优雅"的解决方案,您想要一个吗?还是足够?
PS: There would be more 'elegant' solutions, do you want one? or this is enough?
这篇关于jQuery在foreach循环内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文