2012-04-27 18 views
0

我在我的應用程序中使用jquery自動完成。如何在jquery自動完成中限制用戶可以選擇2的建議?

我想要的是,用戶可以選擇的建議數量應該限制爲2?

我有以下代碼:

<script type="text/javascript"> 
      $(document).ready(function(){ 
       $('#txtTopic').autocomplete('include/ajax.php?option=searchtopics', { 
      minChars: 2, 
      max:10, /* maximum number of records to display at one time */ 
      autoFill: true, 
      multiple: true 
       }); 

      }) 

    </script> 

現在因爲多是真正的用戶能夠選擇2點以上的建議。

他應該只能選擇2條建議。

+0

您準確使用了哪個插件?這些選項是你的例子不支持從'jQuery UI'官方'jQuery Autocomplete' http://jqueryui.com/demos/autocomplete/ – pomeh 2012-04-27 07:32:06

+0

pomesh謝謝你的回覆。我正在使用http://bassistance.de/jquery-plugins/jquery-plugin-autocomplete/ – 2012-04-27 08:09:11

+1

「注意(2010-06-23):此插件已被棄用,不再開發。」如果可以的話,你應該切換到官方插件:) – pomeh 2012-04-27 08:12:34

回答

1

可以使用select事件檢查極限

$('#txtTopic').autocomplete('include/ajax.php?option=searchtopics', { 
    minChars: 2, 
    max:10, /* maximum number of records to display at one time */ 
    autoFill: true, 
    multiple: true, 
    select: function(event, ui) { 
    var terms = split(this.value); 

    if (terms.length <= 2) { 
     // add the selected item 
     terms.pop(); 
     terms.push(ui.item.value); 
     terms.push("");    
    } else { 
     terms.pop(); 
    }    

     this.value = terms.join(", "); 
     return false;  
    } 
    } 
}); 

編輯:上面的代碼片段是jQuery用戶界面自動完成。 LiveDemo

+0

感謝mprabhat回覆我已經提出了修改,但仍然不能正常工作 – 2012-04-27 08:06:26

+0

這段代碼片段假設你使用jQuery UI自動完成 – mprabhat 2012-04-27 08:13:22

相關問題