提交表单之前 jQuery 自动完成强制更改
使用 jquery 我有一个自动完成文本框,如 jQuery UI 网站 http://jqueryui.com 上的组合框示例/demos/autocomplete/#combobox
$("#mycombobox").combobox();
这基本上是一个输入文本框、菜单和按钮。
问题出在 aspx 页面上,我有 mycombobox,上面有一个 asp:button,当按下 Enter 键时,该按钮会连接起来提交表单。
在组合框中
$(<input>)
.autocomplete(...)
.keydown(function (e) {
var keyCode = e.keyCode || e.which;
if (keyCode == 13 || keyCode == 108) {
$(this).blur();
return false;
}
return true;
})
keydown 是我防止它提交的黑客方法,但它只正确了一半。我希望您第一次按 Enter 键时调用自动完成的更改事件,第二次按 Enter 键时可以提交表单。
如果没有 keydown hack,更改仍然会在自动完成时被调用,但直到调用提交之后才会使用旧值。我宁愿完全不这样做,而是寻找更好的方法。
编辑
如果选择了某些内容,则它具有正确的行为,当显示菜单时,我如何手动选择第一个选项?
using jquery I have a autocomplete text box like the combobox example on the jQuery UI website http://jqueryui.com/demos/autocomplete/#combobox
$("#mycombobox").combobox();
This is basically an input textbox, menu, and button.
the problem is on the aspx page I have mycombobox on there is an asp:button that wires up to submit the form when enter is pressed.
in combobox
$(<input>)
.autocomplete(...)
.keydown(function (e) {
var keyCode = e.keyCode || e.which;
if (keyCode == 13 || keyCode == 108) {
$(this).blur();
return false;
}
return true;
})
keydown is my hacky way of preventing it to submit, but it only gets it half right. I want the first time you press enter for the autocomplete's change event to be called and the second time enter is pressed the form can submit.
without the keydown hack, change is still called on the autocomplete but not until after submit is called, using the old value. I would prefer not to do it that way at all and find a better way.
Edit
if something is selected it has the right behavior, how could I manually select the first option when the menu is shown?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以检查自动完成 UI 是否可见,以处理按 Enter 键将执行的操作
you could check to see if the autocomplete UI is visible to handle what pressing enter will do