输入焦点时防止 keyup
prevent keyup when input focus
我打算用空格键触发一些东西,但是当输入焦点时空格键应该被阻止触发。
我正在尝试这种方式,但效果不佳
有什么正确的建议吗?
$(document).ready(function(){
$('input').on('blur', function(){
$(document).on('keyup', function(e){
if(e.keyCode == 32){
alert("pressed!");
}
});
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<label> Name</label>
<input type="text" />
<br />
<label>Pass</label>
<input type="text" />
</form>
在输入有焦点时使用 .off()
分离处理程序:
$(function(){
$(document).on('keyup', function(e) {
if(e.keyCode == 32){
alert("pressed!");
}
});
$('input').on('focus', function() {
$(document).off('keyup');
});
$('input').on('blur', function() {
$(document).on('keyup', function(e) {
if(e.keyCode == 32){
alert("pressed!");
}
});
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<label> Name</label>
<input type="text" />
<br />
<label>Pass</label>
<input type="text" />
</form>
我打算用空格键触发一些东西,但是当输入焦点时空格键应该被阻止触发。
我正在尝试这种方式,但效果不佳 有什么正确的建议吗?
$(document).ready(function(){
$('input').on('blur', function(){
$(document).on('keyup', function(e){
if(e.keyCode == 32){
alert("pressed!");
}
});
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<label> Name</label>
<input type="text" />
<br />
<label>Pass</label>
<input type="text" />
</form>
在输入有焦点时使用 .off()
分离处理程序:
$(function(){
$(document).on('keyup', function(e) {
if(e.keyCode == 32){
alert("pressed!");
}
});
$('input').on('focus', function() {
$(document).off('keyup');
});
$('input').on('blur', function() {
$(document).on('keyup', function(e) {
if(e.keyCode == 32){
alert("pressed!");
}
});
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<label> Name</label>
<input type="text" />
<br />
<label>Pass</label>
<input type="text" />
</form>