我的输入字段<input type="text" id="barcode" placeholder="Barcode"
onkeypress="search(this)">
我想通过对我的js对象中的值按Enter来检查它的值。
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对象的下一个键及其值)
答案 0 :(得分:0)
刚刚测试过False
并尝试过,它工作正常
element.anr as 123
function search(ele) {
if(event.key === 'Enter') {
var anr="123"
// element.anr is the value i want to check my input against
if (ele.value == anr) {
// action that should be performed if value is equal
console.log(ele.value+" - "+anr);
document.getElementById("next").click();
}
}
}
function printHi()
{
console.log("HAIIIII")
}
答案 1 :(得分:0)
您可以使用eventListeners
,
const node = document.getElementsById("barcode");
node.addEventListener("keydown", function(event) {
if (event.key === "Enter") {
event.preventDefault();
// Do more work
}
});