简体   繁体   English

变量显示在一个 function 但没有另一个 Jquery

[英]Variable shows in one function but no another Jquery

I am passing PHP sessions to variables to use in Jquery code.我将 PHP 会话传递给要在 Jquery 代码中使用的变量。 Below, there are four areas where I invoke the console log.下面,我在四个区域调用控制台日志。 Console log numbers 1 and 3 reflect the value of the variables no problem.控制台日志编号 1 和 3 反映的变量值没有问题。 2 and 4 do not. 2和4没有。 I needed #4 to have value so I can use the variables in conditions.我需要 #4 具有价值,以便我可以在条件中使用变量。 I guess this could be a scope issue, but I don't see why.我想这可能是 scope 问题,但我不明白为什么。 Looking for some guidance.寻求一些指导。

<script type="text/javascript">

  // GET VARIABLES THROUGH PHP SESSSION
  customerType = '<?php echo $customerType; ?>';
  myClicks = '<?php echo $myClicks; ?>';
  email = '<?php echo $email; ?>';

  console.log('1 ' + email);
  console.log('1 ' + myClicks);
  console.log('1 ' + customerType);

  //SET NUMBER OF ROWS TO DISPLAY AT A TIME

  $(document).ready(function () {

    rowsPerPage = 5;

    // GETTING DATA FROM FUNCTION BELOW
    getData();

    $('#load-more').click(function () {

      console.log('2 ' + email);
      console.log('2 ' + myClicks);
      console.log('2 ' + customerType);

      $('#load-more').html('Loading...');
      var rowID = Number($('#row-id').val());
      var allCount = Number($('#count').val());
      rowID += rowsPerPage;
      if (rowID <= allCount) {
        $('#row-id').val(rowID);
        getData();
      } else {
        $('#load-more').html('End Of Data');
        //$('#load-more').html('');
      }
    });

    /* REQUEST DATA */
    function getData () {

      console.log('3 ' + email);
      console.log('3 ' + myClicks);
      console.log('3 ' + customerType);

      var rowID = $('#row-id').val();
      var allCount = $('#count').val();
      $.ajax({
        url: 'promotions/newest-load-button-data.php',
        type: 'post',
        data: {
          rowID: rowID,
          rowsPerPage: rowsPerPage
        },
        dataType: 'json',
        success: function (response) {
          setTimeout(function () {
            loadData(response);
            $('#load-more').html('Load More');
          }, 1000);
        }
      });
    }

    /* LOAD DATA TO PAGE */

    function loadData (data) {

      console.log('4 ' + email);
      console.log('4 ' + myClicks);
      console.log('4 ' + customerType);

      var dataCount = data.length;

      for (var i = 0; i < dataCount; i++) {
        if (i == 0) {
          var allCount = data[i]['allcount'];
          $('#count').val(allCount);
        } else {
          var promoID = data[i]['promoid'];
          var promoName = data[i]['promoname'];
          var promoRefNum = data[i]['promorefnum'];
          var promoType = data[i]['promotype'];
          var theBanner = data[i]['thebanner'];

// Here I will use conditions based on email, customerType, and myClicks

          if (promoType == 'Banner') {
            $('#load-container').append('<div class="row-center-center padding-top-5 padding-bottom-2"><div>' + promoName + '</div></div>');
            $('#load-container').append('<div><div class="wrap-content"><img class="mobile-banner-scale" id="visitor-banner-click" src=' + theBanner + '></div></div>');
          }

          if (promoType == 'Video Banner') {
            $('#load-container').append('<div class="row-center-center padding-top-5 padding-bottom-2"><div>' + promoName + '</div></div>');
            $('#load-container').append('<div><video class="mobile-video-size" id="visitor-banner-click" src=' + theBanner + ' autoplay muted loop></video></div>');
          }
        }

        $('#load-more').html('Load More');
      }
    }
  });
</script>

As of jQuery 3.0 the recommended way to add a ready handler is the first method, $(fn).从 jQuery 3.0 开始,添加就绪处理程序的推荐方法是第一种方法 $(fn)。 As noted in the Event section, the $(document).on("ready", fn) event form has slightly different semantics and was removed in jQuery 3.0.如事件部分所述,$(document).on("ready", fn) 事件形式的语义略有不同,并在 jQuery 3.0 中被删除。

This code snippet works using the recommended document ready handler.此代码片段使用推荐的文档就绪处理程序工作。

 jQuery(function(){ //document.ready console.log('2 ' + email); console.log('2 ' + myClicks); console.log('2 ' + customerType); loadData('something'); }); customerType = 'test1'; myClicks = 'test2'; email = 'test3'; function loadData (data) { console.log('4 ' + email); console.log('4 ' + myClicks); console.log('4 ' + customerType); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

It was definitely a scope issue.这绝对是 scope 问题。 I found that if I called the function in the global, I could get the response I needed.我发现如果我在全局调用function,我可以得到我需要的响应。 But there was a data length error.但是出现了数据长度错误。 I decided to call the value of the session on the php data page and include it in the JSON array to pass through ajax along with the other variables that are needed in the load. I decided to call the value of the session on the php data page and include it in the JSON array to pass through ajax along with the other variables that are needed in the load. So it works now, different way.所以它现在可以工作了,不同的方式。 I appreciate everyone's take.我很欣赏大家的看法。

// GET VARIABLES THROUGH PHP SESSSION
customerType = "where are customers";
myClicks = "element clicked";
email = "email sent";

console.log('1 ' + email);
console.log('1 ' + myClicks);
console.log('1 ' + customerType);

//SET NUMBER OF ROWS TO DISPLAY AT A TIME

$(document).ready(function() {

    rowsPerPage = 5;

    // GETTING DATA FROM FUNCTION BELOW
    getData();

    $("#load-more").click(function() {

        console.log('2 ' + email);
        console.log('2 ' + myClicks);
        console.log('2 ' + customerType);

        $('#load-more').html('Loading...');
        var rowID = Number($("#row-id").val());
        var allCount = Number($("#count").val());
        rowID += rowsPerPage;
        if (rowID <= allCount) {
            $("#row-id").val(rowID);
            getData();
        } else {
            $('#load-more').html('End Of Data');
            //$('#load-more').html('');
        }
    });

    /* REQUEST DATA */
    function getData() {
        console.log('3 ' + email);
        console.log('3 ' + myClicks);
        console.log('3 ' + customerType);

        loadData();
    }

    /* LOAD DATA TO PAGE */

    function loadData(data) {
        console.log('4 ' + email);
        console.log('4 ' + myClicks);
        console.log('4 ' + customerType);
    }
});

Everything seems to be fine, your variables email,myClicks,customerType are printing values in console, you need to check once again, I think the issue is something else, Please check once again.一切似乎都很好,您的变量 email,myClicks,customerType 是控制台中的打印值,您需要再次检查,我认为问题是其他问题,请再次检查。

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

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