javascript - 在输入时检查输入字段值对 js 对象值
问题描述
我的输入框<input type="text" id="barcode" placeholder="Barcode"
onkeypress="search(this)">
我想通过在我的 js 对象中按回车键来检查它的值。
function search(ele) {
if(event.key === 'Enter') {
// element.anr is the value i want to check my input against
if (ele.value === element.anr) {
// action that should be performed if value is equal
document.getElementById("next").click();
}
}
};
我在这里做错了什么?当我输入正确的值并按 Enter 键时,什么也没有发生(旁注document.getElementById("next").click();
显示我的 js 对象的下一个键及其值)
解决方案
您可以使用eventListeners
,
const node = document.getElementsById("barcode");
node.addEventListener("keydown", function(event) {
if (event.key === "Enter") {
event.preventDefault();
// Do more work
}
});
推荐阅读
- typescript - Typescript如何在父函数上使用`unknown`类型来确定子函数的类型?
- blazor - 有人 Blazor 远程验证工作吗?
- rust - 返回响应后在后台运行长时间运行的异步函数
- python - 根据 get_input_details 输入图像的 dtype
- javascript - React.js JSX 片段错误和 HTML 缺失标签
- c++ - 是否可以从目标部分更新生成文件变量?
- factory-pattern - 工厂模式是否违反单一责任原则?
- python - Google API OAuth 2.0 redirect_uri_mismatch 错误
- c# - 实体框架的两个跨日期数据列表错误
- docker - 如何使 Identityserver 重定向到我的网络应用程序?