我不知道 jquery 使用哪个 API,如果没有更多信息,我无法提供确切的答案,但我想我可以指出方向。你有几个问题需要克服:
首先,自动完成 API 通常有一个用于选择下拉菜单的事件。这通常是因为元素在自动完成创建它们之前不存在,因此您不能提前编写选择器。jQuery UI 自动完成是这样做的:
"Triggered when an item is selected from the menu;
ui.item refers to the selected item. The default
action of select is to replace the text field's value
with the value of the selected item. Canceling this event
prevents the value from being updated, but does not
prevent the menu from closing."
$( ".selector" ).autocomplete({
select: function(event, ui) { //override this behavior }
});
您的另一个大问题是,如果您无法阻止自动完成触发页面重新加载,则在页面重新加载之间没有有效的客户端通信方式。除非您可以将查询字符串附加到类似.html?reload=true
或类似的 URL,否则您将无法告诉重新加载的页面它已经重新加载。