Jquery 我可以用AJAX在每个循环中放置2个数组吗?
我需要使用2个JSON对象数组,是否可以使用&&分隔符将两个数组放在同一个循环中,或者将每个数组放在每个循环中 我需要在同一个循环中使用2个阵列,这怎么可能呢 这是我的php页面上的JSON:Jquery 我可以用AJAX在每个循环中放置2个数组吗?,jquery,arrays,ajax,Jquery,Arrays,Ajax,我需要使用2个JSON对象数组,是否可以使用&&分隔符将两个数组放在同一个循环中,或者将每个数组放在每个循环中 我需要在同一个循环中使用2个阵列,这怎么可能呢 这是我的php页面上的JSON: function loadBlocSite() { //Création d'un tableau $currentArrayData = array(); //Remplissage du tableau $currentArrayData['configAlm
function loadBlocSite() {
//Création d'un tableau
$currentArrayData = array();
//Remplissage du tableau
$currentArrayData['configAlm'] = array(
array('blocName' => 'blockAlarmeContent', 'blocLVTwoName' => 'boxLV2Content', 'blocHeader' => 'ALARME', 'label' => 'Criticité', 'id' => 'almCrit', 'ico' => '', 'refreshFreq' => '2', 'TNT' => '2', 'FM' => '2'),
array('blocName' => 'blockAlarmeContent', 'blocLVTwoName' => 'boxLV2Content', 'blocHeader' => 'ALARME', 'label' => 'Sans supervision', 'id' => 'almSansSup', 'ico' => '', 'refreshFreq' => '2', 'TNT' => '2', 'FM' => '2'),
array('blocName' => 'blockAlarmeContent', 'blocLVTwoName' => 'boxLV2Content', 'blocHeader' => 'ALARME', 'label' => 'Non nominale', 'id' => 'almNonNom', 'ico' => '', 'refreshFreq' => '2', 'TNT' => '2', 'FM' => '2')
);
$currentArrayData['configSite'] = array(
array('blocName' => 'blockSitesContent', 'blocLVTwoName' => 'boxLV2Content', 'blocHeader' => 'ALARME', 'label' => '-3dB', 'id' => 'steMoins3db', 'ico' => '', 'refreshFreq' => '2', 'TNT' => '4', 'FM' => '1'),
array('blocName' => 'blockSitesContent', 'blocLVTwoName' => 'boxLV2Content', 'blocHeader' => 'ALARME', 'label' => 'Rx', 'id' => 'steRx', 'ico' => '', 'refreshFreq' => '2', 'TNT' => '4', 'FM' => '1'),
array('blocName' => 'blockSitesContent', 'blocLVTwoName' => 'boxLV2Content', 'blocHeader' => 'ALARME', 'label' => 'Perte HF', 'id' => 'stePerteHF', 'ico' => '', 'refreshFreq' => '2', 'TNT' => '4', 'FM' => '1'),
array('blocName' => 'blockSitesContent', 'blocLVTwoName' => 'boxLV2Content', 'blocHeader' => 'ALARME', 'label' => 'Décrochage HS', 'id' => 'steDecrochHS', 'ico' => '', 'refreshFreq' => '2', 'TNT' => '4', 'FM' => '1'),
// array (
// array('blocName' => 'blockSitesContent', 'label' => 'GE Actif En cours', 'id' => 'steGEEnCours', 'ico' => '', 'refreshFreq' => '2', 'val' => '2'),
// array('blocName' => 'blockSitesContent', 'label' => 'GE Actif -10 jrs', 'id' => 'steGEMoins10jrs', 'ico' => '', 'refreshFreq' => '2', 'val' => '2')
// );
);
return $currentArrayData;
}
这是我的密码:
$.ajax({
url: 'Pages/index.php', //ou il y a le traitement json
type: 'get',
dataType: 'json',
data: 'action=loadBlocSite',
success:function(data){
$.each(arrayOfData['configAlm'], function(currentIdx, currentValue){
$.each(arrayOfData['configsite'], function(currentIdx, currentValue){
if(currentValue.id == "almCrit"){
$("#almCrit").click(function() {
$('#mainViewContainer').animate({'left': '-30rem'}, 250, function(){
$('#boxLV2Content').empty();
var criticiteContent = '';
criticiteContent += '<div class="tableHeader">';
criticiteContent += '<div class="tableCell_title">TNT</div>';
criticiteContent += '<div class="tableCell_title">FM</div>';
criticiteContent += '</div>';
criticiteContent += '<div class="lineBloc">';
criticiteContent += '<img class="lineBlocIco" src="'+ currentValue.ico +'">';
criticiteContent += '<div class="lineBlocTitle">'+ currentValue.label +'</div>';
criticiteContent += '<div class="lineBlocValue">'+ currentValue.TNT +'</div>';
criticiteContent += '<div class="lineBlocValue">'+ currentValue.FM +'</div>';
criticiteContent += '</div>';
console.log(currentValue.blocName);
$("#" + currentValue.blocLVTwoName).append(criticiteContent);
});
});
} else return 'this is not almCrit Id';
});
});
}
});
$.ajax({
url:'Pages/index.php',//您需要一个简单的json
键入:“get”,
数据类型:“json”,
数据:“action=loadBlocSite”,
成功:功能(数据){
$.each(arrayOfData['configAlm'],函数(currentIdx,currentValue){
$.each(arrayOfData['configsite'],函数(currentIdx,currentValue){
如果(currentValue.id==“almCrit”){
$(“#almCrit”)。单击(函数(){
$('mainViewContainer')。动画({'left':'-30rem'},250,function(){
$('#boxLV2Content').empty();
var criticiteContent=“”;
临界含量+='';
临界含量+=‘TNT’;
临界内容+=‘FM’;
临界含量+='';
临界含量+='';
临界含量+='';
criticiteContent+=''+currentValue.label+'';
criticiteContent+=''+currentValue.TNT+'';
criticiteContent+=''+currentValue.FM+'';
临界含量+='';
console.log(currentValue.blocName);
$(“#”+currentValue.bloclvtwName).append(criticiteContent);
});
});
}否则返回“这不是almCrit Id”;
});
});
}
});
您将覆盖您的变量。。您需要将代码更改为
$.each(arrayOfData['configAlm'], function(currentIdx, currentValue){
$.each(arrayOfData['configsite'], function(_currentIdx, _currentValue){...
请注意,每次循环第一个数组时,都将循环第二个数组…是的,可以使用嵌套的每个循环,但是,您需要使用不同的参数名称,否则内部循环将覆盖外部循环。这将对外部对象中的每个元素在内部对象上进行迭代,因此我不确定这是您正在寻找的行为,但要做到这一点,您可以这样做
$.each(arrayOfData['configAlm'], function(currentIdx, currentValue){
$.each(arrayOfData['configsite'], function(currentIdx2, currentValue2){...
这两个数组是否都一样长?当两个数组中的值位于同一索引时,它们是否属于一起?您还没有真正解释这两个数组是如何连接的,您的代码显然只是看到在内部循环中声明的
currentValue
,由于外部循环的原因,重复了相同的事情好几次。例如,ico
是第一个或第二个数组对象的属性吗?请为该输入提供示例输入和所需输出。ajax在哪里?@Hitmands我刚刚为ajax调用更新了帖子。您是否更改了循环的内部以反映此处的更改?是的,我完全按照您在代码中提到的方式进行了更改,但不是在问题中。它不会给我任何错误,只是不起作用。什么元素在其中内部循环引用每个数组