简体   繁体   English

依次通过<li>使用 javascript/jquery</li>

[英]Loop through <li> using javascript/jquery

Note: This is a mixture of Php, Javascript and html.注意:这是 Php、Javascript 和 html 的混合物。

I have a code below我在下面有一个代码

            <div class="chat-sidebar-list-wrapper pt-2">
                <h6 class="px-2 pt-2 pb-25 mb-0">MAIDS</h6>
                <ul class="chat-sidebar-list  maid-list-wrapper">
                <?php
                    foreach($all_maids as $all_maid){
                        ?>
                    <!-- displaying looped results in <li> using php foreach method-->
                    <li id="<?php echo $all_maid->id?>" class="bingo">
                     <input type="hidden" id="hidden_receiver_id" name="hidden_receiver_id" value="<?php echo $all_maid->id?>"/>
                        <div class="d-flex align-items-center">
                            <div class="avatar m-0 mr-50"><img src="<?php echo base_url('uploads/'); ?>images/<?php echo get_user_image($all_maid->id)?>" height="36" width="36" alt="sidebar user image">
                                <span class="avatar-status-busy"></span>
                            </div>
                            <div class="chat-sidebar-name">
                                <h6 class="mb-0 maid-names"><?php echo $all_maid->first_name. "" . $all_maid->last_name ?></h6><span class="text-muted">Maid</span>
                            </div>
                        </div>
                    </li>
                    <?php } ?>
                </ul>
              .......
            </div>

What i want is, when you click on each <li> items, you should be able to get the ID from the input tag <input type="hidden" id="hidden_receiver_id" name="hidden_receiver_id" value="<?php echo $all_maid->id?>"/> .我想要的是,当您单击每个<li>项目时,您应该能够从输入标签<input type="hidden" id="hidden_receiver_id" name="hidden_receiver_id" value="<?php echo $all_maid->id?>"/>

So how can i loop the <li> items and get each of it IDs from the input tag?那么我如何循环<li>项目并从输入标签中获取每个 ID?

I tried something below but not working我在下面尝试了一些但没有工作

        var listItems = $(".maid-list-wrapper li");
        listItems.each(function(idx, li) {
            //var id = $(li).text();
            alert($("li.bingo").find("input#hidden_receiver_id").text());
        });

Using jQuery使用 jQuery

var items = $(".chat-sidebar-list li");
items.each(function(id, li) {
    var OneItem = $(li);
    // and the rest of your code.OneItem is an object here
});

The answer is答案是

i added class="hidden_receiver_id" to <input type="hidden" id="hidden_receiver_id" class="hidden_receiver_id" name="hidden_receiver_id" value="<?php echo $all_maid->id?>"/>我将class="hidden_receiver_id"添加到<input type="hidden" id="hidden_receiver_id" class="hidden_receiver_id" name="hidden_receiver_id" value="<?php echo $all_maid->id?>"/>

        $('.maid-list-wrapper li').click(function() {
            //name of each user
            console.log($(this).find("h6.maid-names").text());

             //ID of each user
            //alert($(this).find("input[type='hidden']").val());
            console.log($(this).find("input.hidden_receiver_id").val()) 
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM