我有一张包含大量条目的表格.一旦我在当前文本框中输入值,我想将焦点更改为下一个文本框.并希望将此过程继续到最后一个字段.我的问题是,一旦我在文本框中输入值,是否可以通过javascript编码模拟tab键.
没有按下键盘上的Tab键,我想通过javascript带来相同的功能.这可能吗 ?
我希望浏览器的行为就像用户点击某些内容时按Tab键一样.在点击处理程序中,我尝试了以下方法:
var event = document.createEvent('KeyboardEvent');
event.initKeyEvent("keypress", true, true, null, false, false, false, false, 9, 0);
this.input.focus()[0].dispatchEvent(event);
Run Code Online (Sandbox Code Playgroud)
和jQuery:
this.input.focus().trigger({ type : 'keypress', which : 9 });
Run Code Online (Sandbox Code Playgroud)
第一种方法似乎是最好的选择,但并不是很有效.如果我将最后两个参数更改为98,98,确实在输入框中输入了'b'.但是,9,0和9,9(前者我从MDC网站上取得的)都在FF3下的firebug中给我这些错误:
Permission denied to get property XULElement.popupOpen
[Break on this error] this.input.focus()[0].dispatchEvent(event);
Permission denied to get property XULElement.overrideValue
[Break on this error] this.input.focus()[0].dispatchEvent(event);
Permission denied to get property XULElement.selectedIndex
[Break on this error] this.input.focus()[0].dispatchEvent(event);
Permission denied to set property XULElement.selectedIndex
[Break on this error] this.input.focus()[0].dispatchEvent(event);
Run Code Online (Sandbox Code Playgroud)
我听说过(没有明确定义'此类')事件是"不可信的",这可能解释了这些错误.
第二种方法导致我作为event.which传递的任何值作为event.which,但没有效果(即使我使用98而不是9,在框中没有输入'b'.)如果我尝试设置事件在我传递的对象中的.data,当事件被触发时它最终未定义.以下是我用来查看的代码:
$('#hi').keypress(function(e) {
console.log(e); …Run Code Online (Sandbox Code Playgroud)