duanli9930 2014-07-22 02:26
浏览 37

jquery全局变量未在ajax函数中更新

I have following code:

<script type="text/javascript">
<!-- PAGER
var pag = 1;
var loading = false;
var pag1 = <?php echo $k1; ?>;
var pag2 = <?php echo $k2; ?>;
var max_pages_number = <?php echo $max_pages_number; ?>;
jQuery('body').on('click',"#registry_show_more",function(){
    if(pag < max_pages_number && !loading) {
        loading = true;
        jQuery(this).hide();
        jQuery("#spinner").show();
        alert(pag1 + ' ' + pag2); //alert 1
        jQuery.ajax({
              type: 'POST',
              url: '<?php echo admin_url('admin-ajax.php'); ?>',
              data: {
                action: 'registry_paggination',
                page: pag+1,
                p1: pag1,
                p2: pag2,
                page_size: <?php echo $page_size; ?>,
                category: <?php echo $kategoria; ?>,
                category_slug: '<?php echo $kategoria_slug; ?>',
                filtr: '<?php echo $filtr; ?>',     
                access: <?php echo $access; ?>,
              },
              success: function(data, textStatus, XMLHttpRequest){
                pag++;
                jQuery("#spinner").hide();
                if(pag < max_pages_number) {
                  jQuery('#registry_show_more').show();
                }
                if(data) {
                  jQuery("#newpages").append(jQuery("#newpages").html() +data); 
                  FB.XFBML.parse();
                  jQuery('.open-iframe-lightbox').magnificPopup({type:'iframe'});
                  pag1 = parseInt(jQuery("#p1").attr('value'),10);
                  pag2 = parseInt(jQuery("#p2").attr('value'),10);
                  alert(pag1 + ' ' + pag2); //alert 2
                  jQuery("#p1").remove();
                  jQuery("#p2").remove();
                }
                loading = false;
              },
              error: function(MLHttpRequest, textStatus, errorThrown){
                  jQuery("#spinner").hide();                                        
              }
          });
        }
  });
//-->
</script>

Variables pag, pag1, pag2 update after success function. I know it because i use two alerts to check it. But ajax function send default values. For example: initial values:

pag = 1;
pag1 = 18;
pag2 = 2;

first alert shows:

"18 2"

ajax sends:

 page: 2, 
 p1: 18, 
 p2: 2  
 //skiping the rest of variables

second alert shows(just updated):

 "35 5"

All fine but on the next click:

first alert shows: - ok

"35 5"

ajax sends: - not ok

page: 2, 
p1: 18, 
p2: 2  
//skiping the rest of variables

second alert shows(just updated): - ok, because ajax send the same data as before

"35 5"

I don't know why AJAX function sends every time the same data not new updated values.

I started to think, It seems that the variables in DATA section in AJAX function are not the same as global variables pag,pag1,pag2 or they are constant.

I hope the example is clear enough.

Thanks in advance for your answer

.

.

.

.

Edit: I managed to solve the problem. Problem was something else. I've used. Append () is add new data but when i used. Html () all works properly. Sorry for the inconvenience.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
    • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
    • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
    • ¥50 成都蓉城足球俱乐部小程序抢票
    • ¥15 yolov7训练自己的数据集
    • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
    • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
    • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
    • ¥20 matlab yalmip kkt 双层优化问题
    • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体