某些应用程序必须在Enter press'上进行下一步gui-control。功能。我们使用下面的代码来注册全局处理程序,但它不适用于Bootstrap 3 Datepicker。是否有人建议我们可以覆盖'默认行为?
这是注册keypress事件的全局处理程序的核心:
jQuery.extend(jQuery.expr[':'], {
focusable: function (el, index, selector) {
return $(el).is('a, button, :input, [tabindex]');
}
});
$(document).on('keypress', 'input,select,textarea', function (e) {
if (e.which == 13) {
e.preventDefault();
// Get all focusable elements on the page
var $canfocus = $(':focusable');
var index = $canfocus.index(document.activeElement) + 1;
if ($canfocus.eq(index).attr('tabindex') == -1) index++;
if (index >= $canfocus.length) index = 0;
$canfocus.eq(index).focus();
}
});
我们使用此DP:https://eonasdan.github.io/bootstrap-datetimepicker/
jQuery JavaScript Library是v2.1.4
Bootstrap v3.3.5
Datapicker页面上的代码是:
<form ...>
<div class="form-horizontal">
<div class="form-group">
<label class="col-md-2 control-label" for="Date">Datum</label>
<div class="col-md-10">
<div class="input-group date" id="datetimepicker1">
<input type="text" id="Date" name="Date" >
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
<script type="text/javascript">
$(function () {
$('#datetimepicker1').datetimepicker({ format: 'L' });
});
</script>
<span class="text-danger field-validation-valid"></span>
</div>
</div>
</div>
</form>
使用此代码,在&#39;输入&#39; datapicker聚焦的地方。
答案 0 :(得分:2)
keydown
中有datetimepicker
个事件处理程序停止传播,因此停止了keypress
事件。
一个选项是重载keydown
事件并调用自定义事件,传递which
,然后调用keydown
的原始事件: -
// get original keydown event
var h = jQuery._data($('#datetimepicker1 input')[0], "events").keydown[0].handler;
// override keydown
jQuery._data($('#datetimepicker1 input')[0], "events").keydown[0].handler = function(e) {
// create a new custom event and trigger it with 'which' passed
var event = $.Event('dpkeydown');
event.which = e.which;
$(this).trigger(event);
// call original keydown event
h(e);
}
// now watch for 'dpkeydown'
$(document).on('keypress dpkeydown', 'input,select,textarea', function(e) {
if (e.which == 13) {
.....