自動搜尋填入 autocomplete

資料來源: jQueryui.com

基本款


<link rel="stylesheet" href="//code.jquery.com/ui/1.12.0/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="//code.jquery.com/jquery-1.12.4.js"></script>
<script src="//code.jquery.com/ui/1.12.0/jquery-ui.js"></script>
<script>
$( function() {
    var availableTags = [
      "ActionScript",
      "AppleScript",
      "Asp",
      "BASIC",
      "C",
      "C++",
      "Clojure",
      "COBOL",
      "ColdFusion",
      "Erlang",
      "Fortran",
      "Groovy",
      "Haskell",
      "Java",
      "JavaScript",
      "Lisp",
      "Perl",
      "PHP",
      "Python",
      "Ruby",
      "Scala",
      "Scheme"
    ];
    $( "#tags" ).autocomplete({
      source: availableTags
    });
  } );
</script>

配合 AJAX

$(document).ready(function() {

    $("#select").autocomplete({
        source: function(req, add) {
                var v=$('#select');
                if(v.val().length){
                $.ajax({
                url: 'ajax/select_ajax.php',
                data: {'v':v.val()},
                type:"POST",
                dataType:'json', // PHP傳JSON問題,請參考"AJAX JSON 空白"篇

                success: function(msg){
                    var list = [];
                    $.each(msg, function(i, val) {
                        list.push({
                            label: val['name'],
                            txt: val['id']
                        });
                    });
                    add(list);
                },
            });
            }
        },
        select: function(event, list) {
            $('#txt').val(list.item.txt);
        }
    });
});

results matching ""

    No results matching ""