[英]How can I make Tab and Enter behave the same in a series of Inputs?
I am performing some validation on a series of input boxes. 我正在对一系列输入框执行一些验证。 I need to ensure that a value entered is divisible by 6.
我需要确保输入的值可以被6整除。
When the user tries to submit an invalid value, all other inputs are disabled until they correct the error, and a Div pops up explaining the issue. 当用户尝试提交无效值时,所有其他输入都将被禁用,直到他们纠正错误为止,然后弹出Div解释该问题。
The first approach I tried was capturing a keyup event of a Tab or Enter: 我尝试的第一种方法是捕获Tab或Enter的键入事件:
$(".test1").keyup(function(event) {
if((event.keyCode == 9) || (event.keyCode == 13)) {
event.preventDefault();
var valid = true;
if(parseInt($(this).val()) % 6 != 0){
valid = false;
$('#errorMessage').html("That's not divisible by 6");
}
if (!valid){
// Show error message and disable other text boxes
var position = $(this).position();
$('input').not(this).prop('disabled', true);
$("#validation").show();
$("#validation").offset({top:position.top, left:position.left + $(this).width()});
} else {
// Hide error message and enable other text boxes
$("#validation").delay(200).hide();
$('input').not(this).prop('disabled', false);
$(this).parent().next().find('.test1').focus();
}
}
});
This works fine when the user submits with an Enter, but if they tab it does the following: 当用户使用Enter提交时,此方法很好用,但是如果用户使用Tab键进行了以下操作:
The second approach was to use the Change event: 第二种方法是使用Change事件:
$(".test2").change(function(){
var valid = true;
if(parseInt($(this).val()) % 6 != 0){
valid = false;
$('#errorMessage').html("That's not divisible by 6");
}
if (!valid){
// Show error message and disable other text boxes
var position = $(this).position();
$('input').not(this).prop('disabled', true);
$("#validation").show();
$("#validation").offset({top:position.top, left:position.left + $(this).width()});
} else {
// Hide error message and enable other text boxes
$("#validation").delay(200).hide();
$('input').not(this).prop('disabled', false);
$(this).parent().next().find('.test2').focus();
}
});
This also works fine with Enter, but this time if Tab is pressed after the user has corrected an error, focus is not passed onto the next text box. 这在Enter上也可以正常使用,但是这一次,如果在用户更正错误后按下Tab键,则焦点不会传递到下一个文本框。
See https://jsfiddle.net/bdgriffiths/sqrugh63/3/ 参见https://jsfiddle.net/bdgriffiths/sqrugh63/3/
(I also tried performing the validation using: (我还尝试使用以下方法执行验证:
$('.test').on('input', function() { ...Do the validation... }
Which works fine, but triggers after each keystroke. 效果很好,但是在每次击键后触发。 ie When entering "12" the error would trigger after the "1" was pressed - which would be irritating.)
例如,当输入“ 12”时,错误将在按下“ 1”后触发-这很烦人。)
Turns out it was disabling the other inputs that was screwing this up. 原来是禁用了其他搞砸了的输入。
Solved the issue by creating a CSS class to make the other inputs look disabled: 通过创建CSS类以使其他输入看起来无效来解决该问题:
.disabledClass {
background-color: #eee;
color: #ccc;
border: 1px solid #ccc;
}
And then force the focus to stay in the cell until the validation error is corrected: 然后强制焦点停留在单元格中,直到纠正验证错误为止:
$(this).focus().select();
So the whole function now looks like this: 所以整个功能现在看起来像这样:
$(".test2").blur(function() {
var valid = true;
if (parseInt($(this).val()) % 6 != 0) {
valid = false;
$('#errorMessage').html("That's not divisible by 6");
}
if ((valid) || (!($(this).val()))) { // valid or blank
// Hide error message and enable other text boxes
$("#validation").fadeOut(500);
$('input').not(this).removeClass('disabledClass');
} else { // not valid
// Show error message and disable other text boxes
var position = $(this).position();
$('input').not(this).addClass('disabledClass');
$("#validation").fadeIn(500);
$("#validation").offset({
top: position.top,
left: position.left + $(this).width()
});
$(this).focus().select();
}
});
https://jsfiddle.net/bdgriffiths/sqrugh63/9/ https://jsfiddle.net/bdgriffiths/sqrugh63/9/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.