Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何查找与文本框值匹配的数组的位置号?_Javascript_Arrays_Sorting - Fatal编程技术网

Javascript 如何查找与文本框值匹配的数组的位置号?

Javascript 如何查找与文本框值匹配的数组的位置号?,javascript,arrays,sorting,Javascript,Arrays,Sorting,我希望我的问题不要太不连贯。我是个新手 var inputTextbox = document.getElementById("txtinput"); var outputTextbox = document.getElementById("txtoutput"); var upperGrade = new Array("S", "M", "C", "T") var lowerGrade = new Array("s", "m", "c", "t") if (inputTextbox.valu

我希望我的问题不要太不连贯。我是个新手

var inputTextbox = document.getElementById("txtinput");
var outputTextbox = document.getElementById("txtoutput");

var upperGrade = new Array("S", "M", "C", "T")
var lowerGrade = new Array("s", "m", "c", "t")
if (inputTextbox.value.contains(upperGrade) or   inputTextbox.value.contains(lowerGrade))
{
    var arrayPosition= ??
}
我想检查InputExtBox值是否与数组列表中的值匹配。如果是,我想将匹配值的位置号指定给arrayPosition

如果用户的输入与upperGrade数组列表中的第二个位置(“M”)匹配,那么我想将数字2分配给arrayPosition

我需要使用“if”语句,而不使用循环

var inputTextbox = document.getElementById("txtinput");
var outputTextbox = document.getElementById("txtoutput");

var upperGrade = new Array("S", "M", "C", "T")
//var lowerGrade = new Array("s", "m", "c", "t") no need to have another array just for lower case AS LONG AS IT'S THE SAME LETTERS

var pos = upperGrade.indexOf(inputTextbox.value.toUpperCase()); // pos will be -1 if not found
请注意,索引(位置)从
0
开始。因此
M
将位于位置
1

假设您希望在
outputTextbox

outputTextbox.value = (pos >= 0) ? pos : 'NOT FOUND';
// As RobG said, if you want the 'actual' position, you can use it like this:
// outputTextbox.value = ++pos || 'NOT FOUND';

如果
inputExtBox
中的值位于
upperGrade
数组中,则
outputExtBox
将填充该位置。否则,
notfound
将成为它的内容。

indexOf()
您想要什么?这有意义吗?int index=upperGrade.indexOf(“M”)会为索引赋值2吗?因为“M”是上级数组列表中的第二个位置?或多或少。实际上,索引计数从
0
开始。而且,您总是会得到
1
,因此您应该使用
inputText
中的值。看看我的答案。OP希望索引从1开始,所以
outputTextbox.value=++pos||NOT FOUND'
就可以了。太好了,这正是我想要的!你甚至不需要使用if语句!非常感谢。我不明白为什么你的答案被拒绝,或者为什么我的问题总是被拒绝。我需要让我的代表站起来,这样我就可以给每个人竖起大拇指来反击这里所有的负面人物!