英文:
Korean characters reappear in text <input> after input value cleared and new KR characters typed
问题
我正在创建一个项目,在该项目中,用户键入韩文字符,然后这些字符会显示给他们看。
第一次键入韩文字符时,一切都按预期工作(图1)。但一旦输入字段至少被清除一次,然后再键入另一个韩文字符,之前输入的字符会不希望地出现在新键入的字符之前(图2)。
在这个示例中,当页面加载时,在问题字段中显示了 "ㅐ"。我在键盘上键入 "ㅐ",控制台显示 图1(并且在答案字段中显示 "ㅐ")。在清除输入字段后,问题字段显示 "ㅏ"。我在键盘上键入 "ㅏ",控制台显示 图2,并且答案字段显示 "ㅐㅏ"。
只有在物理键入韩文字符时才会出现此问题,如果键入拉丁字符或将韩文字符粘贴到输入框中,问题不会发生。据我所知,只有在物理键入韩文字符时才会出现这个问题。我发现,通过在键入韩文字符之前按退格键,可以有效地删除先前的字符,不会在新键入的韩文字符之前重新输入。
我正在使用Windows 10家庭版和Google Chrome&Microsoft Edge上的Microsoft IME。
<!-- 开始代码片段:js 隐藏:false 控制台:true Babel:false -->
<!-- 语言:lang-js -->
const questionField = document.querySelector(".questionField");
const inputField = document.querySelector(".inputField");
let currentIndex;
let currentCorrectAnswer;
const characters = [
"ㅏ",
"ㅑ",
"ㅐ"
]
inputField.focus();
generateQuestion();
inputField.addEventListener("input", function(e){
if(inputField.value == currentCorrectAnswer){
correct(e);
} else if(inputField.value != ""){
incorrect(e);
}
})
function correct(e){
console.log(inputField.value + " 输入正确。正在组成: " + e.isComposing);
questionField.innerHTML = "正确"
setTimeout(nextQuestion, 500);
}
function incorrect(e){
console.log(inputField.value + " 输入不正确,答案是 " + currentCorrectAnswer + " 正在组成: " + e.isComposing);
questionField.innerHTML = "不正确"
setTimeout(nextQuestion, 500);
}
function nextQuestion(){
generateQuestion();
inputField.value = "";
}
function generateQuestion(){
currentIndex = Math.floor(Math.random() * characters.length);
questionField.innerHTML = characters[currentIndex];
currentCorrectAnswer = characters[currentIndex];
}
<!-- 语言:lang-html -->
<h1 class="questionField">Key</h1>
<label for="inputField">在键盘上输入此键</label><br>
<input type="text" id="inputField" class="inputField" placeholder="在这里输入答案">
<!-- 结束代码片段 -->
英文:
I am creating a project in which the user types Korean characters that are shown to them.
The first time a Korean character is typed, everything works as intended (fig. 1). But once the input field has been cleared at least once and another Korean character is typed into it, the previously entered character is unwantedly entered right before the newly typed character (fig. 2).
fig. 1 log after a Korean character (ㅐ) is typed after the page loads
fig. 2 log after a Korean character (ㅏ) is entered later
In this example, "ㅐ" is displayed in the question field when the page loads. I enter ㅐ on my keyboard and fig. 1 is displayed in the console (and "ㅐ" is shown in the answer field). After the input field is cleared, the question field displays "ㅏ". I enter ㅏ on my keyboard and fig. 2 is displayed in the console and the answer field displays "ㅐㅏ".
This problem does not occur if Latin characters are typed or if Korean characters are pasted into the input. As far as I can tell the problem only arises if Korean characters are physically typed. I have found that by hitting backspace before typing a Korean character, the previous character is effectively deleted and doesn't get retyped before the newly typed Korean character.
I am using Windows 10 Home and Microsoft IME on Google Chrome & Microsoft Edge.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const questionField = document.querySelector(".questionField");
const inputField = document.querySelector(".inputField");
let currentIndex;
let currentCorrectAnswer;
const characters = [
"ㅏ",
"ㅑ",
"ㅐ"
]
inputField.focus();
generateQuestion();
inputField.addEventListener("input", function(e){
if(inputField.value == currentCorrectAnswer){
correct(e);
} else if(inputField.value != ""){
incorrect(e);
}
})
function correct(e){
console.log(inputField.value + " input & is correct." + " is composing: " + e.isComposing);
questionField.innerHTML = "Correct"
setTimeout(nextQuestion, 500);
}
function incorrect(e){
console.log(inputField.value + " input & is incorrect, " + currentCorrectAnswer + " was the answer." + " is composing: " + e.isComposing);
questionField.innerHTML = "Incorrect"
setTimeout(nextQuestion, 500);
}
function nextQuestion(){
generateQuestion();
inputField.value = "";
}
function generateQuestion(){
currentIndex = Math.floor(Math.random() * characters.length);
questionField.innerHTML = characters[currentIndex];
currentCorrectAnswer = characters[currentIndex];
}
<!-- language: lang-html -->
<h1 class="questionField">Key</h1>
<label for="inputField">Enter this key on your keyboard</label><br>
<input type="text" id="inputField" class="inputField" placeholder="Answer here">
<!-- end snippet -->
答案1
得分: 0
答案字段模糊化并在检查答案后重新聚焦,可以解决这个问题。
这可以通过编辑nextQuestion函数来实现:
function nextQuestion(){
generateQuestion();
inputField.blur();
inputField.focus();
inputField.value = "";
}
英文:
Blurring and re-focusing on the answer field once an answer is checked fixes the issue.
This can be done by editing the nextQuestion function like so:
function nextQuestion(){
generateQuestion();
inputField.blur();
inputField.focus();
inputField.value = "";
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论