Javascript 无法从多维数组中检索值
我有一个多维数组,其中存在一些值 要检索Javascript 无法从多维数组中检索值,javascript,jquery,multidimensional-array,Javascript,Jquery,Multidimensional Array,我有一个多维数组,其中存在一些值 要检索[0][1]或[1][1]索引值。我越来越 未定义为数组,如果我尝试直接获取数组值 我能够得到这个值 这就是我想要达到的目标 我有一个选择下拉菜单,根据我选择的索引 需要从数组框中检索消息。例如,如果索引是 1然后我必须得到[1][1]数组索引值,如果它为零,则[0][1] 数组索引值 这就是我所做的事 getCstMsgName是字符串,而不是数组 一种方法是使用这个 $(document).ready(function () { var te
[0][1]
或[1][1]
索引值。我越来越
未定义为数组,如果我尝试直接获取数组值
我能够得到这个值
这就是我想要达到的目标
我有一个选择下拉菜单,根据我选择的索引
需要从数组框中检索消息。例如,如果索引是
1然后我必须得到[1][1]
数组索引值,如果它为零,则[0][1]
数组索引值
这就是我所做的事 getCstMsgName是字符串,而不是数组 一种方法是使用这个
$(document).ready(function () {
var testName_MessagesArray = new Array();
var cntr = 0;
testName_MessagesArray[cntr] = new Array("testName_custom_message_Label1val", "Custom message for label 1");
cntr++;
testName_MessagesArray[cntr] = new Array("testName_custom_message_Label2val", "Custom message for label 2");
cntr++;
alert(testName_MessagesArray[1][1]);
var classChk = $(".customCheckEnabled");
alert(classChk);
this.testName = testName_MessagesArray; //<-- set this with the name
var getClassindex = classChk.attr("selectedIndex");
alert(getClassindex);
var getVarName = classChk.attr("id");
alert(getVarName);
var getCstMsgName = this[getVarName]; //<-- reference it from this
alert(getCstMsgName);
var getMessage = getCstMsgName[getClassindex][1];
alert(getMessage);
});
$(文档).ready(函数(){
var testName_MessagesArray=new Array();
var-cntr=0;
testName_MessagesArray[cntr]=新数组(“testName_custom_message_Label1val”,“标签1的自定义消息”);
cntr++;
testName_MessagesArray[cntr]=新数组(“testName_custom_message_Label2val”,“标签2的自定义消息”);
cntr++;
警报(testName_MessagesArray[1][1]);
var classChk=$(“.customCheckEnabled”);
警报(classChk);
this.testName=testName\u MessagesArray;//请参阅此更新:
您应该真正使用数组文字,而不是,呃,cntr
s:
var testName_MessagesArray = [
["testName_custom_message_Label1val", "Custom message for label 1"],
["testName_custom_message_Label2val", "Custom message for label 2"]
];
然后,如果要从中检索值,请使用testName\u MessagesArray[x][y]
你在做什么:
var classChk=$(".customCheckEnabled");
// this is a jQuery element, alerted as [object Object]
var getClassindex=classChk.attr("selectedIndex");
// this is 0 or 1
var getVarName=classChk.attr("id");
// this will be the id of the first selected element, a string
var getCstMsgName=getVarName+"_MessagesArray".toString();
// this will create a string, from the "getVarName"-string and your string-literal-toString
var getMessage=getCstMsgName[getClassindex][1];
// as "getCstMsgName" is a string - not the twodimensional array,
// getCstMsgName[getClassindex] returns the character at the selected index (as a string)
// and getCstMsgName[getClassindex][1] results in the second character of the one-character-string - undefined
为什么要在数组中创建数组?只需在索引中添加一个值!@Bergi我无法从array@p0rter这个多维数组也在其他地方使用,这就是为什么它会这样创建。要将键与值匹配,该数组已经存在于数据库中,我无法修改,您可以创建吗以小提琴为例
var classChk=$(".customCheckEnabled");
// this is a jQuery element, alerted as [object Object]
var getClassindex=classChk.attr("selectedIndex");
// this is 0 or 1
var getVarName=classChk.attr("id");
// this will be the id of the first selected element, a string
var getCstMsgName=getVarName+"_MessagesArray".toString();
// this will create a string, from the "getVarName"-string and your string-literal-toString
var getMessage=getCstMsgName[getClassindex][1];
// as "getCstMsgName" is a string - not the twodimensional array,
// getCstMsgName[getClassindex] returns the character at the selected index (as a string)
// and getCstMsgName[getClassindex][1] results in the second character of the one-character-string - undefined