简体   繁体   English

Jquery“点击”或“解除绑定”不适用于php循环

[英]Jquery "On click" OR"unbind" not working with php loop

I am working with Jquery Ajax and php,I am using Ajax in my controller, I have php loop and inside loop i have "showmore button",I have "ShowMore" button and with "Last Comment"(coming from db),So whenever i click on "ShowMore" button...its hide and "New comments" and new "ShowMore" button (HTML) is coming as Ajax Resonse But Problem is whenever i click on first time on "ShowMore" Button then its working and data (New Comments and show more button) is coming as ajax response but if i click on next time on "ShowMore"(which is coming as Ajax response) then nothing works,I tried with following functions我正在使用 Jquery Ajax 和 php,我在控制器中使用 Ajax,我有 php 循环和内部循环我有“showmore 按钮”,我有“ShowMore”按钮和“最后一条评论”(来自 db),所以每当我点击“ShowMore”按钮......它的隐藏和“新评论”和新的“ShowMore”按钮(HTML)将作为Ajax Resonse出现但问题是每当我第一次点击“ShowMore”按钮然后它的工作和数据(新评论和显示更多按钮)以 ajax 响应的形式出现,但如果我下次点击“ShowMore”(以 Ajax 响应的形式出现),则没有任何效果,我尝试使用以下功能

$('.show_more').unbind('click').bind('click', function (e)

with above function "second click" not working means "show more button"(which is coming as ajax response) not working And if tried with following code then multiple times function work/execute使用上述功能“第二次点击”不起作用意味着“显示更多按钮”(作为ajax响应出现)不起作用如果尝试使用以下代码,则多次函数工作/执行

$(document).on('click','.show_more',function()

Here is my code in loop and my script code,Where i am wrong ?这是我的循环代码和脚本代码,我错在哪里? How can i solve this ?我该如何解决这个问题? Thanks in advance.提前致谢。

function fetch()
    {
        $FeedId; (dynamic)
        $FeedData = $this->M_main->GetFeedData(); // getting feed data from database
        foreach($FeedData as $feeds)
            {
                if($feeds['flag']=="feed")
                    {
                        $GetFeedComments = $this->M_main->GetFeedsComment($FeedId);
                        $TotalFeedsComments=$GetFeedComments['TotalRows'];
                        if($TotalFeedsComments>1)
                                {
                                        $Loads='<div class="show_more_main" id="show_more_main'.$postID.'">
                                        <input type="hidden" name="fixs" value="'.$postID.'" id="fixs">
                                        <input type="hidden" name="MinValue" value="'.$postID.'" id="MinValue">
                                        <input type="hidden" name="FeedIdd" value="'.$FeedId.'" id="FeedIdd">
                                        <input type="hidden" name="MaxValue" value="'.$postID.'" id="MaxValue">
                                        <span id='.$postID.' data-val='.$postID.' data-status='.$postID.' class="show_more" title="Load more posts" data-feds='.$FeedId.'>Show more</span>
                                        <span class="loding" style="display: none;"><span class="loding_txt">Loading...</span></span>
                                                            </div>';
                                }
                        
                    }
            }
    $pathss= base_url()."Main/GetFeedCommentsById"; 
             echo "
                <script>
                $('.show_more').unbind().click(function(e) {
                e.preventDefault(); 
                    var ID = $(this).attr('id');
                        var vals=$(this).data('val');
                        var feds=$(this).data('feds');
                        $('.show_more').hide();
                    $.ajax({
                            type:'POST',
                            url:'".$pathss."',
                            data:{id:ID, vals:vals},
                            success:function(html){
                                $('#show_more_main'+ID).remove();
                                $('.postList'+feds).append(html);
                            }
                        });
                    
                    });
                </script>
                ";

The problem is you are removing the html node on ajax success message, and the event listener goes away.问题是您正在删除 ajax 成功消息上的 html 节点,并且事件侦听器消失了。

You should add the listener again to the new node;您应该再次将侦听器添加到新节点;

<script>
// Assign the event listener to a variable, to reuse it
let myEventListenerFunction = function(e) {
    e.preventDefault(); 
    var ID = $(this).attr('id');
        var vals=$(this).data('val');
        var feds=$(this).data('feds');
        $('.show_more').hide();
    $.ajax({
            type:'POST',
            url:'".$pathss."',
            data:{id:ID, vals:vals},
            success:function(html){
                $('#show_more_main'+ID).remove();
                $('.postList'+feds).append(html);
                // Add event to a new node
                $('.show_more').unbind().click(myEventListenerFunction);
            }
        });
    
}
$('.show_more').unbind().click(myEventListenerFunction);
</script>

There are some problem with your code but if you just want to make it work as it is the best way in my opinion is to separate the javascript part from the rest.您的代码存在一些问题,但如果您只是想让它工作,因为我认为最好的方法是将 javascript 部分与其他部分分开。 One way to achieve that is to add this script in your page into a separate js file实现此目的的一种方法是将此脚本添加到您的页面中到单独的 js 文件中

<script>
$(document).on('click','.show_more',function(e) {
                e.preventDefault(); 
                        var $self = $(this);
                        var ID = $self.attr('id');
                        var vals=$self.data('val');
                        var feds=$self.data('feds');
                        $('.show_more').hide();
                        var url = $self.data('url');
                        
                    $.ajax({
                            type:'POST',
                            url: url,
                            data:{id:ID, vals:vals},
                            success:function(html){
                                $self.remove();
                                $('.postList'+feds).append(html);
                            }
                        });
                    
                    });
</script>

And in the php part you have to had one attribute like this在 php 部分,你必须有一个这样的属性

$Loads='<div class="show_more_main" id="show_more_main'.$postID.'" data-url="'.$pathss.'">...</div>';

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

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