是否有一个jQuery自动完成插件,它会强制选择一个项目?

自动完成function ,但它不会强制选择项目。 我需要这样的东西,但它必须强制选择一个项目才能“提交”。

它存在吗?

这是我用.blur的解决方案(而且我使用名称| ID对)

jQuery("#username").autocomplete( "/yii_app/stock/suggestUser", { 'mustMatch':true, 'multiple':false, 'formatItem':function(result, i, num, term) { return ''+result[1]+'   '+ result[0]; } } ).result( function(event,item) { $("#user_id").val(item[1]); } ).blur( function() { $("#user_id").val(''); $(this).search(); } ); 

user_id是隐藏的输入字段,username是文本输入字段ajax结果使用以下格式:user1name | user1id \ n user2name | user2id \ n

您可以使用自动填充的“更改”事件

  var availableTags = [ "ActionScript", "AppleScript" ]; $("#tags").autocomplete({ source: availableTags, change: function (event, ui) { if(!ui.item){ //http://api.jqueryui.com/autocomplete/#event-change - // The item selected from the menu, if any. Otherwise the property is null //so clear the item for force selection $("#tags").val(""); } } }); 

使用选项:

 mustMatch:true, 

这可以在Bassistance自动完成插件中找到。 我不知道它是否是jQuery自动完成或Bassistance自动完成的一部分,但它的工作原理!

这是我目前使用jQuery UI 自动完成的解决方案:

  $('#some-input').autocomplete({ source: 'some-url', autoFocus: true, minLength: 2, select: function(event, ui) { //remember the selected item $('#some-input') .data('selected-item', ui.item.label); } }).blur(function() { var value = $('#some-input').val(); //check if the input's value matches the selected item if(value != $('#some-input').data('selected-item')) { //they don't, the user must have typed something else $('#some-input') .val('') //clear the input's text .data('selected-item', ''); //clear the selected item } }); 

每次触发模糊事件时,都会检查输入的值是否先前已被选中。 如果尚未选中(您知道因为select事件从未触发),则“selected-item”变量将与输入的值不匹配。 然后你可以做任何你想做的事情(我清除输入,但你可以尝试其他的东西)。

将它与validation插件结合使用。 只需创建validation插件required的字段即可。

您还可以使用内置的提交事件

 $('Autocomplete').autocomplete({ select: function(event, ui) { $('submit').show(); } });