[英]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.