简体   繁体   English

使用.indexOf()找不到数组索引

[英]Can't find the array index using .indexOf()

I am writing a program that takes all of the text from a text box, parses it into an array removing white-space, and then will search the array for expected values. 我正在编写一个程序,它从文本框中获取所有文本,将其解析为一个删除空格的数组,然后搜索数组中的预期值。

I can test it and get the program to successfully take the text in the text box and parse it into the array, but when I try to find the expected values in the array, it throws an error. 我可以测试它并使程序成功获取文本框中的文本并将其解析到数组中,但是当我尝试在数组中找到期望的值时,它会抛出错误。 The error is, "Object doesn't support this property or method". 错误是“对象不支持此属性或方法”。

JavaScript: JavaScript的:

function generateOutputfvoc()
{
    var inputArr = document.getElementById("inputBox").value.split(/[\s]/);
    var nameLoc = inputArr.indexOf("Name");
    document.getElementById("contactNameOutput").innerHTML = nameLoc;
}

HTML snippet: HTML片段:

<p>Paste Text Here</p>
<textarea rows='8' cols='152' id='inputBox' placeholder="Copy text and paste here" wrap="off"></textarea>
<form><input type='button' onclick='generateOutputfvoc()' value='Generate Output' /></form>
<p>Contact Name: <b id='contactNameOutput'></b></p>

Thank you. 谢谢。

您可以根据以下页面使用javascript的原型功能添加indexOf(): https//developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/indexOf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM