如何动态获取按钮ID?

时间:2017-03-11 07:26:32

标签: javascript php jquery ajax codeigniter

大家好我想尝试获取一个for循环的按钮ID,所以每当按钮单击可以区别于其他ID的唯一ID时,应该能够正常工作。让我告诉你我的代码。

 <?php foreach ($message as $key ) : ?>
                                    <?php echo $senders_user_id=$key['senders_id']; ?>
                                    <div class="senders_id"  S_id="<?php echo $key['senders_id']; ?>" style="display: none;"></div>
                            <a  class="list-group-item">
                            <div class="checkbox">
                            <label>
                             <?php echo form_open(,['id'=>'sender_user_id']); ?>
                                <input type="checkbox">
                                <button id="senders_id" class="Read" S_id="<?php echo $key['senders_id'];?>" onclick="readbyuser($senders_user_id);">Read</button>
                                <button  id="senders_id" class="unread"  S_id="<?php echo $key['senders_id'];?>" onclick="unreadbyuser($senders_user_id);">Unread</button> 

                            </label>
                            </div>           

                        <span class="glyphicon glyphicon-star-empty"></span><span class="name" style="min-width: 120px;
                            display: inline-block;"><?php echo $key['uname']; ?></span> <span class=""><?php echo $key['textdata']; ?></span>
                        <span class="text-muted" style="font-size: 11px;"></span> <span class="badge">12:10 AM</span> <span class="pull-right"><span class="glyphicon glyphicon-paperclip">
                        <?php echo form_close(); ?>
                            </span></span></a>
                                        <?php endforeach; ?>
                    </div>

现在这里有三个帖子意思是三个&#39; senders_id&#39;所以,当我点击按钮时,读取该ID的ID应该通过,但无论我点击哪个按钮,同一个id都会一次又一次地传递

这是我的代码

function readbyuser()
{
    var senders_id=$('#senders_id').attr('S_id');
    var Recievers_id=$('.Recievers_id').attr('R_id');
jQuery.ajax({
            type:'post',
            url:'<?php echo base_url("user/read_by_user"); ?>',
            data:{id:senders_id,R_id:Recievers_id},
            dataType:'json',
            success:function(data)
            {
                console.log(data);

                alert(data);

                $('.unread').removeClass('unread_user');
                $('.Read').addClass('read_user');


            }


        });

}
function unreadbyuser()
{
var senders_id=$('#senders_id').attr('S_id');
    var Recievers_id=$('.Recievers_id').attr('R_id');
jQuery.ajax({
            type:'post',
            url:'<?php echo base_url("user/unread_by_user"); ?>',
            data:{id:senders_id,R_id:Recievers_id},
           dataType:'json',

            success:function(data)
            {



  $('.Read').removeClass('read_user');
    $('.unread').addClass('unread_user');
    }
});
}

请告诉我我做错了什么

1 个答案:

答案 0 :(得分:0)

<button id="senders_id" class="Read" S_id="<?php echo $key['senders_id'];?>" onclick="readbyuser($senders_user_id);">Read</button>


<button  id="senders_id" class="unread"  S_id="<?php echo $key['senders_id'];?>" onclick="unreadbyuser($senders_user_id);">Unread</button> 

两个元素不能具有相同的id。改为使用类名。

您也可以直接在此传递PHP值。

onclick="readbyuser($senders_user_id);"

作为

onclick="readbyuser(<?php echo $key['senders_id'];?>);"