[英]check input field value on enter press against js object value
我的输入字段<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对象的值)
刚测试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") }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="text" id="barcode" placeholder="Barcode" onkeypress="search(this)"> <button id="next" onclick="printHi();">test</button>
您可以使用eventListeners
,
const node = document.getElementsById("barcode");
node.addEventListener("keydown", function(event) {
if (event.key === "Enter") {
event.preventDefault();
// Do more work
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.