jQuery ajax回调函数停止后追加接收到的数据和接收到的jQuery脚本不工作

jQuery ajax callback function stop after append received data and received jquery script dont work

本文关键字:jQuery 数据 脚本 工作 ajax 回调 追加 函数      更新时间:2023-09-26

我有两个问题与jQuery ajax回调。我有以下jQuery ajax调用代码:

                  <div id="newpages"></div>
                  <script type="text/javascript">
                  <!--
                    var page = 1;
                    var max_pages_number = <?php echo $max_pages_number; ?>;
                      jQuery(document).ready(function(){
                        jQuery("#registry_show_more").click(function(){
                          if(page < max_pages_number) {
                              jQuery.ajax({
                                    type: 'POST',
                                    url: '<?php echo admin_url('admin-ajax.php'); ?>',
                                    data: {
                                      action: 'registry_paggination',
                                      page: page+1,
                                      plusid: 18,  
                                      page_size: <?php echo $page_size; ?>,
                                      category: <?php echo $kategoria; ?>,
                                      filtr: '<?php echo $filtr; ?>',     
                                      access: <?php echo $access; ?>,
                                    },
                                    success: function(data, textStatus, XMLHttpRequest){
                                      page++;
                                      if(page >= max_pages_number) {
                                        jQuery("#registry_show_more").hide();
                                      }
                                      if(data) {
                                        alert('a');
                                        jQuery("#newpages").append(jQuery("#newpages").html() +data);
                                        alert('b');
                                      }
                                    },
                                    error: function(MLHttpRequest, textStatus, errorThrown){}
                                });
                              }
                           });
                        });
                  //-->
                  </script>
                  <div style="display: block;" id="registry_show_more">
                    <a class="w-blog-entry-more g-btn type_default size_small">WCZYTAJ WIĘCEJ</a>
                  </div>

第一个问题。收到数据后,在ajax回调警报('a')显示,然后数据被附加到div与id="newpages"。但是第二个警报('b')没有显示。

第二个问题。在ajax.php文件中,我有部分代码如下:

                                        <script type="text/javascript">                                         
                                        <!-- plus
                      jQuery(document).ready(function(){
                        jQuery("#bp<?php echo $row->registry_id; ?>").click(function(){
                          if(!jQuery.cookie('regvote<?php echo $row->registry_id; ?>')) {
                              jQuery.cookie('regvote<?php echo $row->registry_id; ?>','+', { expires: 1 });
                              jQuery.ajax({
                                    type: 'POST',
                                    url: '<?php echo admin_url('admin-ajax.php'); ?>',
                                    data: {
                                      action: 'registryVote',
                                      plusid: <?php echo $row->registry_id; ?>,
                                    },
                                    success: function(data, textStatus, XMLHttpRequest){
                                      jQuery("#vc<?php echo $row->registry_id; ?>").html(data);
                                    },
                                    error: function(MLHttpRequest, textStatus, errorThrown){}
                                });
                              }
                           });
                        });
                      //-->
                                        </script>
                                    <div class="post twc-post type-post status-publish format-standard hentry w-blog-entry">
                                          <div id="votepanel<?php echo $row->registry_id; ?>" style="width: 35px; float: left;">
                                            <a style="cursor:pointer; cursor:hand" id="bp<?php echo $row->registry_id; ?>"><i class="fa fa-plus-square" style="cursor: hand !important; color: #bd2029; font-size: 32px;"></i></a>
                                            <h6 id="vc<?php echo $row->registry_id; ?>" style="margin: 0; padding: 0;"><?php echo number_format((double)$row->value,1); ?></h6>
                                           <a style="cursor:pointer; cursor:hand" id="bm<?php echo $row->registry_id; ?>"><i class="fa fa-minus-square-o" style="color: #bd2029; font-size: 22px;"></i></a>
                  </div>
                                        <script type="text/javascript">                                         
                                        <!-- minus
                      //jQuery(document).ready(function(){
                        jQuery("#bm<?php echo $row->registry_id; ?>").click(function(){
                          if(!jQuery.cookie('regvote<?php echo $row->registry_id; ?>')) {
                              jQuery.cookie('regvote<?php echo $row->registry_id; ?>','-', { expires: 1 });
                              jQuery.ajax({
                                    type: 'POST',
                                    url: '<?php echo admin_url('admin-ajax.php'); ?>',
                                    data: {
                                      action: 'registryVote',
                                      minusid: <?php echo $row->registry_id; ?>,
                                    },
                                    success: function(data, textStatus, XMLHttpRequest){
                                      jQuery("#vc<?php echo $row->registry_id; ?>").html(data);
                                    },
                                    error: function(MLHttpRequest, textStatus, errorThrown){}
                                });
                              }
                            });
                       // });
                      //-->
                                        </script>

和脚本MINUS和PLUS不工作,当我把它们附加到div "newpages"。

我不完全确定,但我认为第一个问题是原因,因为你不能使用追加追加一个html文本。你可以附加元素,但直接html不是一个好主意,我认为。尝试使用jQuery("#newpages").html(jQuery("#newpages").html() + data);代替

关于第二个问题,据我所知,你是追加这些脚本到一个文件后调用?不是文档之后的调用。已经准备好了吗?您正确地附加了脚本,但是脚本在文档之后执行。准备好了哪些已经过去了?抱歉,如果我没有理解正确的时间顺序。