[英]Button not clicking in HTML and JavaScript
My question is the following in JavaScipt and HTML:我的问题是 JavaScipt 和 HTML 中的以下问题:
let submit = document.getElementById("button");
let text = document.getElementById("inputForm");
submit.addEventListener('сlick', function () {
let textValue = text.value;
console.log("The input is " + textValue)
});
I am sure I connected the HTML to JS correctly via the script attribute.我确定我通过脚本属性将 HTML 正确连接到 JS。
<div class="input">
<h3 id="inputText">Ввод:</h3>
<input id="inputForm" />
<button id="button">Добавить</button>
</div>
Here is the part of the Code I want to work, but the Button has no effects.这是我想要工作的代码部分,但按钮没有效果。 Initially I was putting a lot of fonts on a button and input field in css, but then I deleted them and it still doesn't work.
最初我在 css 的按钮和输入字段上放了很多 fonts,但后来我删除了它们,它仍然不起作用。 By work I mean to print the input of the input field to the console.
通过工作,我的意思是将输入字段的输入打印到控制台。
I don't know what it was, but you had some weird character in there I think... all I did was removing the click
and the bracket from the AddEventListener
function and rewrite it.我不知道它是什么,但我认为你有一些奇怪的字符......我所做的只是从
AddEventListener
function 中删除click
和括号并重写它。 Strange, I have to admit.奇怪,我不得不承认。 Just
textValue.Value
is wrong it needs to be lowercase textValue.value
otherwise it was completely fine.只是
textValue.Value
是错误的,它需要是小写textValue.value
否则完全没问题。 For all reading, this create a snippet and try run with textValue.value
this was not the mistake.对于所有阅读,这将创建一个片段并尝试使用
textValue.value
运行,这不是错误。 The event listener wasn't set up somehow.事件侦听器没有以某种方式设置。
let submit = document.getElementById("button"); let text = document.getElementById("inputForm"); submit.addEventListener("click", function () { let textValue = text.value; console.log("The input is " + textValue) });
<div class="input"> <h3 id="inputText">Ввод:</h3> <input id="inputForm" /> <button id="button">Добавить</button> </div>
Edit: so I couldn't leave this open, because many of you and myself were confused by which char could it be.编辑:所以我不能让这个打开,因为你们中的许多人和我自己都对它可能是哪个字符感到困惑。 I analyzed it in Notepad++ and wanted to see all chars but there was no invisible char like CR or LF.
我在 Notepad++ 中对其进行了分析,并希望查看所有字符,但没有像 CR 或 LF 这样的不可见字符。 My next thought was the encoding because @vnikonov_63 was writing cyrillic chars inside his html.
我的下一个想法是编码,因为@vnikonov_63 在他的 html 中写入西里尔字符。 What I did was transform the code to Windows-1251 ( https://de.wikipedia.org/wiki/Windows-1251 ) and there you can see the result...
我所做的是将代码转换为 Windows-1251 ( https://de.wikipedia.org/wiki/Windows-1251 ),在那里你可以看到结果......
submit.addEventListener('СЃlick', function () {
Everything is the same but not the c
.一切都相同,但
c
。 I compared Windows-1251 (cyrillic) and Windows-1250 ( https://de.wikipedia.org/wiki/Windows-1250 Middle European) Encodings and the c
has the exact same Position. I compared Windows-1251 (cyrillic) and Windows-1250 ( https://de.wikipedia.org/wiki/Windows-1250 Middle European) Encodings and the
c
has the exact same Position. So all of this is just some encoding issue.所以所有这些都只是一些编码问题。 Surely a
c
which is not really a c
as javascript expects it, won't set up a eventlistener because javascript doesn't know a event called СЃlick
. Surely a
c
which is not really a c
as javascript expects it, won't set up a eventlistener because javascript doesn't know a event called СЃlick
. As I am not an expert with encodings i can't explain to you why the СЃ
shows up as an c
but i am pretty sure that was the problem.由于我不是编码专家,我无法向您解释为什么
СЃ
显示为c
但我很确定这是问题所在。
I would work with the form tag.我会使用表单标签。 There is an event for submit when you click the submit button which is type of submit.
当您单击提交类型的提交按钮时,会有一个提交事件。
code:代码:
document.querySelector("form").addEventListener("submit", (e) => { var text = document.querySelector("#inputForm"); console.log(text.value); });
<form action="javascript:void(0);"> <h3 id="inputText">Ввод:</h3> <input id="inputForm" /> <button type="submit">Добавить</button> </form>
The issue is with the Value property.问题在于 Value 属性。 It should be.value.
应该是.value。
let submit = document.getElementById("button");
let text = document.getElementById("inputForm");
submit.addEventListener('click', function () {
let textValue = text.value;
console.log("The input is : " + textValue);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.