Nat*_*ong 7 jquery javascript-events
在jQuery中,如何触发用户标签到下一个输入字段的行为?
我试过这个:
var e = jQuery.Event("keydown");
e.which = 9; // # Key code for the Tab key
$("input").trigger(e);
Run Code Online (Sandbox Code Playgroud)
但是触发事件不会将光标移动到下一个字段.
我想我可以手动移动光标focus(),但是决定接下来应该是哪个字段是浏览器已经知道该怎么做的事情,所以触发标签似乎更清晰.
有任何想法吗?
这是一种解决方案,通过http://jqueryminute.com/set-focus-to-the-next-input-field-with-jquery/
$.fn.focusNextInputField = function() {
return this.each(function() {
var fields = $(this).parents('form:eq(0),body').find(':input').not('[type=hidden]');
var index = fields.index( this );
if ( index > -1 && ( index + 1 ) < fields.length ) {
fields.eq( index + 1 ).focus();
}
return false;
});
};
Run Code Online (Sandbox Code Playgroud)
使用方法如下:
$( 'current_field_selector' ).focusNextInputField();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
27492 次 |
| 最近记录: |