javascript - 变量显示在一个函数中,但没有另一个 Jquery
问题描述
我将 PHP 会话传递给要在 Jquery 代码中使用的变量。下面,我在四个区域调用控制台日志。控制台日志编号 1 和 3 反映的变量值没有问题。2和4没有。我需要 #4 具有价值,以便我可以在条件中使用变量。我想这可能是范围问题,但我不明白为什么。寻求一些指导。
<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>
解决方案
这绝对是一个范围问题。我发现如果我在全局中调用该函数,我可以得到我需要的响应。但是出现了数据长度错误。我决定在 php 数据页面上调用会话的值,并将其包含在 JSON 数组中,以通过 ajax 以及加载中所需的其他变量。所以它现在可以工作了,不同的方式。我很欣赏大家的看法。
推荐阅读
- android - 将 int 数组绑定到列表视图
- javascript - javascript动态添加多组文本框并将它们全部重复
- php - 在 drupal8 中使用特定术语保存分类术语 id
- google-translate - Google Cloud Translator API 归属的自定义协议
- c++ - 调查小型转储而不从进程中获取所有二进制文件
- swift - tableView 重新加载但不更新/更改 UI
- kotlin - Kotlin RxJava2 Flowable PublishSubject 不发出数据 onNext
- java - Sns @NotificationMessage 不适用于不同于 String 的类型
- vb.net - vb.net 使用变量动态创建表单名称
- sql - 如何查询给定数字中所有数字的总和?