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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
    • ¥20 Java-Oj-桌布的计算
    • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
    • ¥20 有人知道这种图怎么画吗?
    • ¥15 pyqt6如何引用qrc文件加载里面的的资源
    • ¥15 安卓JNI项目使用lua上的问题
    • ¥20 RL+GNN解决人员排班问题时梯度消失
    • ¥60 要数控稳压电源测试数据
    • ¥15 能帮我写下这个编程吗
    • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路