Php 如何使用Jquery从列表组行获取特定项值
我有一个包含许多行的列表组,每行有两个以上的数据,每个项都有一个id,我希望如果我单击这样的行,我可以获得一个自定义值,但我使用的方式会提供所有行的值。如果尝试将其更改为items id,则仅对listGrou的第一行位置0有效。请帮帮我, , Html和php代码:Php 如何使用Jquery从列表组行获取特定项值,php,jquery,html,Php,Jquery,Html,我有一个包含许多行的列表组,每行有两个以上的数据,每个项都有一个id,我希望如果我单击这样的行,我可以获得一个自定义值,但我使用的方式会提供所有行的值。如果尝试将其更改为items id,则仅对listGrou的第一行位置0有效。请帮帮我, , Html和php代码: <div id="listid"> <ul class="list-group p-5 col-sm-8 offset-2" id="content"> <?php while
<div id="listid">
<ul class="list-group p-5 col-sm-8 offset-2" id="content">
<?php
while ($registered = mysql_fetch_array($query_student)) {
?>
<li class="list-group-item list-group-item-action Divlistid" >
<a href="#">
<span id="name">
<h4><b>
<?php echo $registered['firstname']; ?>
</b></h4>
</span>
<span id="gender">
<?php echo $registered['gender']; ?>
</span>
</li>
<?php
}
?>
</ul>
</div>
通过索引确定您的li目标:
$(".list-group-item").('click',function(){
var liIndex = $(this).index();
if( liIndex === 0 ){ // i'm the first li from .list-group }
// target last - logical
if( liIndex === $(this).parent().children('.list-group-item').length -1 ){ // i'm the last li from .list-group }
// target last - hardcoded
if( liIndex === $('.list-group .list-group-item').length -1 ){ // i'm the last li from .list-group }
})
DOM中不能有多个具有相同ID的元素。例如,您可以尝试将这些ID与循环计数器连接起来,并使用“class”属性将目标元素与jquery连接起来。这不是PHP问题,请删除PHP代码,并提供生成的HTML示例。您好,很抱歉,您不清楚要从列表中获取什么,你能举一个好结果的例子吗?请不要在带有class.list group项的元素上使用id属性,还有,你的span元素在循环中有一个常量id,因此你将有多个重复id's@RiggsFolly ,我放了一个屏幕截图,你可以看到结果现在我已经合理地格式化了代码,你看到你从HTML中漏掉了什么,使其格式良好吗?谢谢,但我的主要问题不是获取列表组的索引,而是每当单击一行时获取列表组每行的特定值
$(".list-group-item").('click',function(){
var liIndex = $(this).index();
if( liIndex === 0 ){ // i'm the first li from .list-group }
// target last - logical
if( liIndex === $(this).parent().children('.list-group-item').length -1 ){ // i'm the last li from .list-group }
// target last - hardcoded
if( liIndex === $('.list-group .list-group-item').length -1 ){ // i'm the last li from .list-group }
})