[英]Button not clicking in HTML and JavaScript
我的問題是 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)
});
我確定我通過腳本屬性將 HTML 正確連接到 JS。
<div class="input">
<h3 id="inputText">Ввод:</h3>
<input id="inputForm" />
<button id="button">Добавить</button>
</div>
這是我想要工作的代碼部分,但按鈕沒有效果。 最初我在 css 的按鈕和輸入字段上放了很多 fonts,但后來我刪除了它們,它仍然不起作用。 通過工作,我的意思是將輸入字段的輸入打印到控制台。
我不知道它是什么,但我認為你有一些奇怪的字符......我所做的只是從AddEventListener
function 中刪除click
和括號並重寫它。 奇怪,我不得不承認。 只是textValue.Value
是錯誤的,它需要是小寫textValue.value
否則完全沒問題。 對於所有閱讀,這將創建一個片段並嘗試使用textValue.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) });
<div class="input"> <h3 id="inputText">Ввод:</h3> <input id="inputForm" /> <button id="button">Добавить</button> </div>
編輯:所以我不能讓這個打開,因為你們中的許多人和我自己都對它可能是哪個字符感到困惑。 我在 Notepad++ 中對其進行了分析,並希望查看所有字符,但沒有像 CR 或 LF 這樣的不可見字符。 我的下一個想法是編碼,因為@vnikonov_63 在他的 html 中寫入西里爾字符。 我所做的是將代碼轉換為 Windows-1251 ( https://de.wikipedia.org/wiki/Windows-1251 ),在那里你可以看到結果......
submit.addEventListener('СЃlick', function () {
一切都相同,但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. 所以所有這些都只是一些編碼問題。 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
. 由於我不是編碼專家,我無法向您解釋為什么СЃ
顯示為c
但我很確定這是問題所在。
我會使用表單標簽。 當您單擊提交類型的提交按鈕時,會有一個提交事件。
代碼:
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>
問題在於 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.