简体   繁体   中英

How can I disable the key in second time press same in jQuery?

When press first time 37 it showing alert message, again press the same key second time I want to disable that key. How can do that?

$(document).on (‘keydown’ , function (e){
var userVal = e.which ∥ e.keycode
valadation (userVal)

})

function valadation (userVal){
if (37 == userVal){
alert (“Wecome”)
 }
}

Create a variable and update its value.

 let clickVar = 0 $(document).on('keydown', function(e) { var userVal = e.which || e.keycode valadation(userVal) }) function valadation(userVal) { if (37 === userVal && clickVar === 0) { alert('Welcome'); clickVar++ } }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

 //name the handler $(document.body).on('keydown', function alertOnEnter (e) { if ([e.keyCode, e.which].indexOf(13) > -1) { //remove the handler from the event $(document.body).off('keydown', alertOnEnter); alert('you hit enter'); } });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM