当用户单击 html div (#searchboxquicktab) 时,我希望光标移动到输入框,并且它应该模拟按下 Enter 键,以便它可以仅显示单击的结果。
这是 html:
<div class="form-type-textfield form-item-title form-item form-group">
<input placeholder="Start typing a name here to filter listing..." class="form-control form-text ctools-auto-submit-processed" id="edit-title" name="title" value="" size="30" maxlength="128" type="text">
<div id="searchboxquicktab">
<span class="glyphicon glyphicon-search"></span>Search
</div>
</div>
搜索框 (#searchboxquicktab) 由 javascript 呈现,因此下面的 jquery 代码中的父元素和子元素
// Select textbox and press enter key to show results.
$('.form-item-title').on("click", "#searchboxquicktab", function() {
console.log('search box quicktab button pressed.');
var e = jQuery.Event("keydown");
e.which = 13; // Enter
$('input.ctools-auto-submit-processed').focus().trigger(e);
});
但这不起作用。
最佳答案
触发按键事件,将被监听它们的任何东西接收,但它们不会影响输入。我已经测试了您的代码,并且该事件实际上正在被触发。
也就是说,我过去使用了一个名为 jquery.sendkeys.js
的 jQuery 插件(请参阅 https://github.com/dwachss/bililiteRange/blob/master/jquery.sendkeys.js )来模拟击键。
注意:jquery.sendkeys.js
需要 bililiteRange.js
。
https://github.com/dwachss/bililiteRange/blob/master/bililiteRange.js .
关于javascript - jQuery onclick,选择输入框并按回车键(渲染html),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26291181/