0

アプリケーションでjqueryオートコンプリートを使用しています。

私が欲しいのは、ユーザーが選択できる提案の数を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つの提案しか選択できないはずです。

4

1 に答える 1

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 UI オートコンプリート用です。ライブデモ

于 2012-04-27T07:31:16.397 に答える