获取 foreach PHP 中元素 HTML 的索引


get index of element html within of foreach php

我英语不好,对不起,如果我在这里写得不好......

我使用了 php 的 foreach,里面有一个标签,里面有一个名为 .....并且确实使一键显示元素的索引,如何?

例如

   <? foreach($tipo_persona as $persona){ ?>
         <label id="lb_person" class="simulasublink"><? echo $persona["tipo"]; ?> </label><br>
    <? } ?>

我可以使用 id="'.$i.'" 和 for($i = 0;$i <= ....).. 获取 ID,但我想要其他方式 ..我已经在jquery中阅读了每个()的一些内容,但我还没有完成我想要的

适合。

谢谢大家..

在小提琴 http://jsfiddle.net/sosgenru/

$('.simulasublink').each(function(i) {
    $(this).on('click',function() {
        alert("this is the "+i+" element");
    });
});

也不要在标签中使用相同的 id,因为它会产生不准确的代码,因为 id 是唯一的

 <? $i=0; foreach($tipo_persona as $persona){ ?>
         <label id="lb_person<? echo $i; ?>" class="simulasublink"><? echo $persona["tipo"]; ?> </label><br>
    <? $i++;} ?>