如果没有可用数据,则rails3-jquery-autocomplete清除字段

我正在使用rails3-jquery-autocomplete。 如果自动完成没有返回结果,是否可以清除自动填充字段的值? 我试图阻止用户提交我们在数据库中没有的值。

我使用的是旧版本,但我为autocomplete()调用实现了一个change处理程序。

 $('input[data-autocomplete]').live('focus', function(i){ $(this).autocomplete({ // ... change: function(event, ui) { var $this = $(this); // blank if nothing was selected if (!ui.item) { $this.val(null); var id_selector = $this.attr('id_element'); if (id_selector) $(id_selector).val(null); } } }); }); 

您可以使用select回调函数来检查值是否正确,如果不是则清除此输入

 $( ".selector" ).autocomplete({ select: function(event, ui) { ... } });