简体   繁体   English

从文本输入字段中获取文本

[英]Get text from text input field

I've got this code here:我在这里有这段代码:

 <input type="text" class="todo-input" placeholder="Gebe hier dein Taskein">
 <button onclick="task = document.getElementsByClassName('todo-input').value; 
 window.alert(task)">Hinzufügen</button>

But why do I geht undefined and not the text in the input field?但是为什么我是未定义的,而不是输入字段中的文本? And what do I have to do that I get the input text?我该怎么做才能获得输入文本?

document.getElementsByClassName returns an array of elements. document.getElementsByClassName 返回一个元素数组。

To get the value you want to retrieve the element by index, eg:要通过索引获取要检索元素的值,例如:

task = document.getElementsByClassName('todo-input')[0].value;

Also, you should consider using an ID instead of a class.此外,您应该考虑使用 ID 而不是 class。 IDs are meant to be unique, classes are not. ID 是唯一的,类不是。 eg例如

<input type="text" id="myInput" class="todo-input" placeholder="Gebe hier dein Taskein">
task = document.getElementById('myInput').value;

Because you are selecting by class name, you must define the element number of which you want to select, since JavaScript returns a collection of elements otherwise.因为您是通过 class 名称选择的,所以您必须定义您想要 select 的元素编号,因为 JavaScript 否则返回元素集合。 In your current code, JavaScript is unable to determine with element with a class name of "todo-input" to select, so it'll always return undefined.在您当前的代码中,JavaScript 无法确定 class 名称为 select 的元素,因此它总是返回未定义。

Since the input box is the only element on the page with the "todo-input" class, you can use the modified code below in order to select the specific element and get its value.由于输入框是页面上唯一带有“todo-input” class 的元素,因此您可以使用下面的修改代码来 select 特定元素并获取其值。 Remember, counting starts at 0 in JavaScript, so this code will select the first input on the page that has the specified class name.请记住,在 JavaScript 中计数从 0 开始,因此此代码将 select 是页面上具有指定 class 名称的第一个输入。

    <input type="text" class="todo-input" placeholder="Gebe hier dein Taskein">
 <button onclick="task = document.getElementsByClassName('todo-input')[0].value; 
 window.alert(task)">Hinzufügen</button>

As seen in the modified code above, the selector is now paired with [0], which will effectivly select the textbox on the page and display it's value.如上面修改后的代码所示,选择器现在与 [0] 配对,这将有效地 select 页面上的文本框并显示它的值。

document.getElementsByClassName('todo-input')[0].value

If your page will contain additional elements with the same class name or you add a new element with the same class before the current element, make sure to adjust the number value so you're selecting the right element.如果您的页面将包含具有相同 class 名称的其他元素,或者您在当前元素之前添加具有相同 class 的新元素,请确保调整数值以便选择正确的元素。

Please check the following link.请检查以下链接。 https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByClassName https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByClassName

getElementsByClassName returns the array-like object not object. getElementsByClassName 返回array-like object而不是 object。

 <input type="text" class="todo-input" placeholder="Gebe hier dein Taskein"> <button onclick="task = document.getElementsByClassName('todo-input')[0].value; window.alert(task)">Hinzufügen</button>

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

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