我是jQuery的新手并希望有人可以帮助我?
我有这样的多维数组(如果错误,请更正)
var arr = {};
arr['AAA'] = {
'show': 'showTableA',
'txt' : 'txtHintA',
'url' : 'a.php'
};
arr['BBB'] = {
'show': 'showTableB',
'txt' : 'txtHintB',
'url' : 'b.php'
};
arr['CCC'] = {
'show': 'showTableC',
'txt' : 'txtHintC',
'url' : 'c.php'
};
如何像这样循环函数多维数组?我是从here
得到的function arr['AAA']['show'](str) {
if (str=="") {
document.getElementById(arr['AAA']['txt']).innerHTML="";
return;
}
xmlhttp.onreadystatechange=function() {
if (this.readyState==4 && this.status==200) {
document.getElementById(arr['AAA']['txt']).innerHTML=this.responseText;
}
}
xmlhttp.open("GET", arr['AAA']['url']+"?q="+str,true);
xmlhttp.send();
}
function BBB
function CCC
and so on ...
提前谢谢
答案 0 :(得分:0)
如果你稍微改变它会起作用。您可以在添加XmlHttpRequest后进行测试。
var arr = {};
arr['AAA'] = {
'show': 'showTableA',
'txt' : 'txtHintA',
'url' : 'a.php'
};
arr['BBB'] = {
'show': 'showTableB',
'txt' : 'txtHintB',
'url' : 'b.php'
};
arr['CCC'] = {
'show': 'showTableC',
'txt' : 'txtHintC',
'url' : 'c.php'
};
console.log(arr);
function showTable(key, str) {
if (str=="") {
document.getElementById(arr[key]['txt']).innerHTML="";
return;
}
/*xmlhttp = new XmlHttpRequest();
xmlhttp.onreadystatechange=function() {
if (this.readyState==4 && this.status==200) {
document.getElementById(arr[key]['txt']).innerHTML=this.responseText;
}
}
xmlhttp.open("GET", arr[key]['url']+"?q="+str,true);
xmlhttp.send();*/
document.getElementById(arr[key]['txt']).innerHTML=str;
}
$.each( arr, function( key, value ) {
showTable(key, "test");
console.log( key + ": " + value );
});

<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<p id="txtHintA"></p>
<p id="txtHintB"></p>
<p id="txtHintC"></p>
</body>
</html>
&#13;