如何更改键盘输入并在输入字段中显示更改的数据

How to change input from keyboard and display changed data in the input field

我的 HTML 代码中有一个输入元素,我用键盘输入了一些值。然后我使用 onkeypress 处理程序更改此值并显示键盘值和更改后的值。

如何将其更改为仅显示更改后的值?

function dialogTrigger(event) {
  var keyboardInput = event.which;
  keyboardInput = keyboardInput + 5;
  var changedInput = String.fromCharCode(keyboardInput);
  $("#fake-search").val($("#fake-search").val() + changedInput);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="fake-search" onkeypress="dialogTrigger(event)" class="fake-search" type="text" />

您可以防止事件默认行为:

function dialogTrigger(event) {
  event.preventDefault(); //<<-- here e.g
  var keyboardInput = event.which;
  keyboardInput = keyboardInput + 5;
  var changedInput = String.fromCharCode(keyboardInput);
  $("#fake-search").val($("#fake-search").val() + changedInput);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="fake-search" onkeypress="dialogTrigger(event)" class="fake-search" type="text" />