加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 获取迭代的ul内容的唯一ID

发布时间:2020-12-13 16:55:07 所属栏目:PHP教程 来源:网络整理
导读:我在Codeigniter中使用 PHP并且更新鲜.我在最近2天遇到了问题. 我有一个页面来显示从数据库中获取并使用循环显示的事件列表.我成功地完成了这件事.现在我想给出一个选项来编辑或删除应该进行db更改的单个事件.为此,我使用2个按钮;编辑和删除.注意所有都在一
我在Codeigniter中使用 PHP并且更新鲜.我在最近2天遇到了问题.

我有一个页面来显示从数据库中获取并使用循环显示的事件列表.我成功地完成了这件事.现在我想给出一个选项来编辑或删除应该进行db更改的单个事件.为此,我使用2个按钮;编辑和删除.注意所有都在一个循环中,因此在每个事件(项目)中重复相同的按钮.点击一个按钮后,它会显示一个模态.例如,当单击“删除”按钮时,它会显示一个要求确认删除的模式.该模态中的DELETE按钮提交我的表单并调用应该删除相应数据库条目的控制器函数.

如何识别该特定项目的ID,以便我可以删除或更新它?我总是得到第一个项目的ID.我提到这个:passing php variable to modal window with click function

但我无法将这些与我的问题联系起来

这是我的观点:

<ul class="timeline">

    <?php  
    if(isset($eventlist)){ 
      foreach ($eventlist as $events) { 
        <li class="yellow timeline-noline' > 
            <div class="timeline-time">
                        <span class="date" id="date" >
                        <?php if(!empty($events->event_date)){echo $events-   >event_date ;} ?> </span>
                                        <span class="time" id="time" >
                                        <?php if(!empty($events->event_time)){echo $events->event_time ;}?> </span>
                                    </div>
                                    <div class="timeline-icon">
                                        <i class="icon-trophy" style="margin-top:12px;"></i>
                                    </div>
                                    <div class="timeline-body">
                                    <div>
                                    <button type="button" title="Remove" id="remove" name="remove" href="#portlet-remove" data-toggle="modal" style="float:right;background-color: transparent;border: 0px;" ><i class="fa fa-trash-o"></i></button>
                                    <button type="button" title="edit" id="edit" name="edit"  href="#portlet-edit"  data-toggle="modal"style="float:right;background-color: transparent;border: 0px;" ><i class="fa fa-pencil-square-o"></i></button>
                                        <h2 id="title" ><?php if(!empty($events->event_title)){echo $events->event_title ;}?></h2>
                                    </div>

                                        <div class="timeline-content">
                                            <img class="timeline-img pull-left" src="<?php echo $this->config->base_url(); ?>assets/admin/pages/media/blog/2.jpg" alt="">
                                        <span id="desc" > <?php if(!empty($events->event_desc)){echo $events->event_desc ;}?>   </span>
                                        </div>
                                        <div class="timeline-footer">
                                            <a href="javascript:;" class="nav-link pull-right">
                                            <!--Read more <i class="m-icon-swapright m-icon-white"></i> -->
                                            </a>
                                        </div>
                                    </div>
                                </li>

                                <?php

                            }  }  ?>

                            </ul>

解决方法

在你的标记ID按钮重复.这将导致html验证错误,如果您尝试访问带ID的按钮,它将始终指向具有相应ID的第一个元素.

你可以在删除按钮中使用用户定义的属性,如下所示,也可以使用class而不是ID.

<button type="button" title="Remove" rel="id for identifying element $i " class="remove" name="remove" href="#portlet-remove" data-toggle="modal" style="float:right;background-color: transparent;border: 0px;" ><i class="fa fa-trash-o"></i></button>

Javascript喜欢这个

$('body').on('click','.remove',function(){
         var idUnique =   $(this).attr('rel');
         //You could pass this idUnique to controller for deletion.

    //Assign the idUnique to Hidden field inside modal body.
$('#selectedID').val(idUnique );
    });

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读