doumeng1143 2017-06-09 10:31
浏览 76
已采纳

Jquery没有处理AJAX响应

I have three sections in my working code:

  1. a PHP file,
  2. a jQuery code section which contains an AJAX function which requests,
  3. another PHP file.

The response is a HTML section which should be prepended to a DIV inside the first PHP file (from where AJAX was requested using the jQuery section in the <script> tag inside the <head> section of the PHP file.

My query is while AJAX is prepending dynamically the HTML successfully in the desired DIV element id="content", the other click events targeted with response HTML element tags are not working dynamically.
What could be the reason?

$(".update_button").click(function (){
    var updateval = $("#update").val();
    var cate = $("#selectcategory").val();
    var up = $("#uploadvalues").val();
    if (up)
    {
        var uploadvalues = $("#uploadvalues").val();
    }
    else
    {
        var uploadvalues = $(".preview:last").attr('id');
    }
    var X = $('.preview').attr('id');
    if (X)
    {
        var Z = X + ',' + uploadvalues;
    }
    else
    {
        var Z = 0;
    }
    var dataString = 'update=' + updateval + '&Category=' + cate + '&uploads=' + Z;
    if ($.trim(updateval).length == 0 || $("#selectcategory").val() === "")
    {
        if ($.trim(updateval).length == 0)
        {
            alert('Please Enter SOME TEXT!');
        }
        else if ($("#selectcategory").val() === "")
        {
            alert('Choose a Category from the list!');
        }
    }
    else
    {
        $.ajax({
            type: "POST",
            url: $.base_url + "message_ajax.php",
            data: dataString,
            cache: false,
            success: function (html)
            {
                $("#flash").fadeOut('slow');
                $(".teacherpost").prepend(html);
                $("#update").val('').focus();
                $("#selectcategory").val('');
                $('#preview').html('');
                $('#uploadvalues').val('');
                $('#photoimg').val('');
                $(".decoration").hide();
            }
        });
    }
    return false;
});

HTML prepended from AJAX resposne:

<div class="post-description">
    <div class="stats">
        <div class="st_like_share">
            <a class="Like" href="javascript:void(0)" id='like<?php echo $msg_id;?>' title='<?php echo $like_status;?>' rel='<?php echo $like_status;?>' data='<?php echo $shareKey; ?>'><i class="glyphicon glyphicon-star" style="color:#F16522;"></i><?php  echo  $like_count; ?></a>
            <?php  } else {    ?>  
            <a class="Like" href="javascript:void(0)" id='like<?php echo $msg_id;?>' title='<?php echo $like_status;?>' rel='<?php echo $like_status;?>' data='<?php echo $shareKey; ?>'><i class="glyphicon glyphicon-star" ></i><?php echo  $like_count;  ?></a>              
            <?php } ?>                                         
            <a href="javascript:void(0)" class="stat-item remark" data-id="<?php 
                echo $msg_id;?>"  title='Give your Remarks on it'>
            <i class="fa fa-comments-o icon"></i><?php echo  $remark_count;  ?>
            </a>                          
            <a href="javascript:void(0)" class="stdelete stat-item" data-id="<?php echo $omsg_id;?>"  rel="" title="Delete Post">
            </a>
            <?php }  
                ?>
        </div>
    </div>
</div>
<div class="post-footer" id="post-footer-<?php echo $msg_id;?>">
    <textarea name="comment" class="form-control add-comment-input" id="ctextarea<?php echo $omsg_id;?>" style="width:100%;" placeholder="Add a remark here..."></textarea>
    <span class="input-group-btn">
        <input type="submit"  value="Remark" name="Remark"  id="<?php echo $omsg_id;?>" rel="<?php echo $msg_id;?>" class="btn btn-info pull-right Remark_button">
        <!--<input  type="submit" id=""  value="Send" class="btn btn-default go-chat pull-right ">-->
    </span>
    <?php if($remark_count>0)   { ?>
    <ul class="comments-list" id="commentload<?php echo $msg_id;?>">
        <?php include('comments_load.php'); ?>
    </ul>
    <?php } ?>
</div>
</div>

jQuery which is not working on the AJAX responded HTML code:

/*Remark Load */
$(".stat-item.remark").on("click", function( e ) {
    var post_footer_id = "#post-footer-"+$(this).data('id');
    $(post_footer_id).slideToggle();
});

Dynamically this is not working, but when I refresh the page this jQuery works fine.

  • 写回答

2条回答 默认 最新

  • doumaqing6652 2017-06-09 10:40
    关注

    Try like this

    $(document).on('click', '.stat-item.remark', function(){ 
             var post_footer_id = "#post-footer-"+$(this).data('id');
          $(post_footer_id).slideToggle();
      }); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接