【发布时间】:2018-08-02 09:43:39
【问题描述】:
我使用 PHP 生成了一个列表,其中包含从数据库中选择的元素。它工作得很好。但问题是当我尝试使用 jquery 获取 li 索引时,它只会给我奇数索引。就像当我点击第一个元素时,它会给出这些索引:
home(index 0: it should be 0)
,about(index 2: actually it should be 1)
,about(index 4: actually it should be 2)
,about(index 6: actually it should be 3)
,about(index 8: actually it should be 4)
,about(index 10: actually it should be 5).
我已经使用 while 循环来生成这个id。
<ul style="list-style:none;">
<?php while($row = $result->fetch_assoc()):?>
<li class="list-group-item"><?=$row['name']?><li>
<?php endwhile ?>
</ul>
JS:
$('ul li').click(function() {
var index = $(this).index();
console.log(index);
})
【问题讨论】:
-
请发布一些相关的代码/html来理解问题。还可以通过更正索引来明确您的目标