Korean characters reappear in text <input> after input value cleared and new KR characters typed

huangapple go评论76阅读模式
英文:

Korean characters reappear in text <input> after input value cleared and new KR characters typed

问题

我正在创建一个项目,在该项目中,用户键入韩文字符,然后这些字符会显示给他们看。

第一次键入韩文字符时,一切都按预期工作(图1)。但一旦输入字段至少被清除一次,然后再键入另一个韩文字符,之前输入的字符会不希望地出现在新键入的字符之前(图2)。

图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(&quot;.questionField&quot;);
const inputField = document.querySelector(&quot;.inputField&quot;);
let currentIndex;
let currentCorrectAnswer;
const characters = [
    &quot;ㅏ&quot;,
    &quot;ㅑ&quot;,
    &quot;ㅐ&quot;
]

inputField.focus();
generateQuestion();

inputField.addEventListener(&quot;input&quot;, function(e){
    if(inputField.value == currentCorrectAnswer){
        correct(e);
    } else if(inputField.value != &quot;&quot;){
        incorrect(e);
    }
})

function correct(e){
    console.log(inputField.value + &quot; input &amp; is correct.&quot; + &quot; is composing: &quot; + e.isComposing);
    questionField.innerHTML = &quot;Correct&quot;
    setTimeout(nextQuestion, 500);
}

function incorrect(e){
    console.log(inputField.value + &quot; input &amp; is incorrect, &quot; + currentCorrectAnswer + &quot; was the answer.&quot; + &quot; is composing: &quot; + e.isComposing);
    questionField.innerHTML = &quot;Incorrect&quot;
    setTimeout(nextQuestion, 500);
}

function nextQuestion(){
    generateQuestion();
    inputField.value = &quot;&quot;;
}

function generateQuestion(){
    currentIndex = Math.floor(Math.random() * characters.length);
    questionField.innerHTML = characters[currentIndex];
    currentCorrectAnswer = characters[currentIndex];
}

<!-- language: lang-html -->

&lt;h1 class=&quot;questionField&quot;&gt;Key&lt;/h1&gt;
&lt;label for=&quot;inputField&quot;&gt;Enter this key on your keyboard&lt;/label&gt;&lt;br&gt;
&lt;input type=&quot;text&quot; id=&quot;inputField&quot; class=&quot;inputField&quot; placeholder=&quot;Answer here&quot;&gt;

<!-- end snippet -->

答案1

得分: 0

答案字段模糊化并在检查答案后重新聚焦,可以解决这个问题。

这可以通过编辑nextQuestion函数来实现:

function nextQuestion(){
    generateQuestion();
    inputField.blur();
    inputField.focus();
    inputField.value = &quot;&quot;;
}
英文:

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 = &quot;&quot;;
}

huangapple
  • 本文由 发表于 2023年7月3日 19:20:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76604248.html
  • cjk
  • html
  • javascript

如何使用` :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定