1

我想使用 jQuery、PHP 和 MySQL 在我的表单中使用多个自动完成功能。但这对我不起作用。我有什么错误吗?


HTML 代码:

<input type="text" id="country" name="country" />

jQuery代码:

$( "#countries" )
        // don't navigate away from the field on tab when selecting an item
        .bind( "keydown", function( event ) {
            if ( event.keyCode === $.ui.keyCode.TAB &&
                $( this ).data( "autocomplete" ).menu.active ) {
                event.preventDefault();
            }
        })

        .autocomplete({
            source: function( request, response ) {
                $.getJSON( "process/find_countries.php", {
                    term: extractLast( request.term )
                }, response );
            },
            search: function() {
                // custom minLength
                var term = extractLast( this.value );
                if ( term.length < 2 ) {
                    return false;
                }
            },
            focus: function() {
                // prevent value inserted on focus
                return false;
            },
            select: function( event, ui ) {
                var terms = split( this.value );
                // remove the current input
                terms.pop();
                // add the selected item
                terms.push( ui.item.value );
                // add placeholder to get the comma-and-space at the end
                terms.push( "" );
                this.value = terms.join( ", " );
                return false;
            }
        });  

PHP 代码

    $result = array();
    $term = strtolower($_GET["term"]);
    $sql = "SELECT title FROM tbl_countries WHERE title LIKE ?";
    $q = $db->prepare($sql);
    $q->execute(array('%'.$term.'%'));
    $rows = $q->rowCount();
    echo($rows);
    while ($r = $q->fetch())
    {
        array_push($result,array('label'=>$r['title'], 'value'=>$r['title']));
    }
    echo json_encode($result);
4

1 回答 1

2

在服务器的萤火虫响应中看到。以及为什么要做 echo($rows); ?

于 2012-07-27T13:41:10.997 回答