自动完成不显示准确的答案,它显示所有选项


autocomplete does not display accurate answer it displays all options

我有一个页面,我必须在选择州下拉列表中显示地区下拉列表。 然后在选择Distirict 下拉列表时显示邮局下拉列表。 然后自动完成将输入框放在选择邮局下拉菜单上。

我成功完成了它。 但是地点的自动完成输入框不会根据输入的关键字显示选项,天气它会显示输入的所有关键字选项。

下面是我的PHP页面:

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>jQuery UI Autocomplete - Default functionality</title>
    <link rel="stylesheet" href="js/jquery-ui.css">
    <script src="js/jquery.js"></script>
    <script src="js/jquery-ui.js"></script>
    <link rel="stylesheet" href="style.css">
</head>
<body>
<?php
    $r = file('app_data/in.txt');
    $states = array();
    foreach($r as $value){
        $x = explode("'t",$value);
        //$india[] = array('pin'=>$x[1], 'place' => $x[2], 'state' => $x[3], 'dist' => $x[5] );
        $states[] = $x[3];
    }
    $states = array_unique($states);
    sort($states);
    echo '<select id="state">';
    foreach($states as $state){
        echo "<option> $state </option>";
    }
    echo '</select>';
    foreach($_POST as $post){
        echo $post;
    }
    echo '<select id="dist">';
    echo '</select>';
    echo '<select id="postoffice">';
    echo '</select>';
?>
<script>
    $('#state').focusout(function (){
        $.get('func-get-dist.php',{'state' : $(this).val()},function(data){
            $('#dist').html(data);
        });
    });
    $('#dist').focusout(function (){
        $.get('func-get-dist.php',{'state' : $('#state').val(),'dist' : $(this).val()},function(data){
            $('#postoffice').html(data);
        });
    });
    $('#postoffice').focusout(function (){
            var postoffice = $(this).val();
            $('#tags').autocomplete({
                source : ("func-get-dist.php?postoffice="+postoffice),
            });
    });

</script>
<div class="ui-widget">
    <label for="tags">Tags: </label>
    <input id="tags">
</div>
<div id="test">
<div id="test1">
</div>
</body>
</html>

下面是我的html-get-dist.php页面:

<?php 
if(isset($_GET['postoffice']) && !empty($_GET['postoffice'])){
    $postoffice = $_GET['postoffice'];
    $r = file('app_data/in.txt');
    $places = array();
    foreach($r as $value){
        $x = explode("'t",$value);
        if(strtolower($x[7]) == $postoffice){
            $places[] = strtolower(trim($x[2]));
        }
    }
    $places = array_unique($places);
    sort($places);
    echo json_encode($places);
}elseif(isset($_GET['state']) && !empty($_GET['state'])){
    if(isset($_GET['dist']) && !empty($_GET['dist'])){
        $state = $_GET['state'];
        $dist = $_GET['dist'];
        $r = file('app_data/in.txt');
        $posts = array();
        foreach($r as $value){
            $x = explode("'t",$value);
            if(($x[3] == $state) && ($x[5] == $dist)){
                //echo $x[6].'-'.$x[7].'-'.$x[8].'<br>';
                $posts[] = $x[7];
            }
        }
        $posts = array_map('strtolower',$posts);
        $posts = array_unique($posts);
        sort($posts);
        foreach($posts as $postoffice){
            echo '<option>'.$postoffice.'</option>';
        }
    }else{
        $state = $_GET['state'];
        $r = file('app_data/in.txt');
        $dists = array();
        foreach($r as $value){
            $x = explode("'t",$value);
            if($x[3] == $state){
                $dists[] = $x[5];
            }
        }
        $dists = array_unique($dists);
        sort($dists);
        foreach($dists as $dist){
            echo '<option>'.$dist.'</option>';
        }
    }
}
?>

您将用户输入与整个值进行比较,尝试使用与用户输入的字符数相同的字符数进行比较:

foreach($r as $value){
    $x = explode("'t",$value);
    $charCount = strlen($postoffice);
    if(strtolower(substr($x[7], $charCount)) == strtolower($postoffice)){
        $places[] = strtolower(trim($x[2]));
    }
}

编辑:我花了一些时间来弄清楚,但我认为我有解决方案。代码中有 2 个错误:

  • 在 js 中,您可以获得带有 var postoffice = $(this).val(); 的 select 的值,但.val()返回 selectbox 的选定索引,而不是所选文本。
  • 您将所选邮局与$x[7]进行比较,这是数据中的第 8 个制表符分隔列,如果我的计数正确,您那里只有 7 列。

所以我的猜测是,在这里您将undefined0进行比较,返回true

if(strtolower($x[7]) == $postoffice){

要解决这个问题,请像这样获取 js 中的值:

 var postoffice = $("option:selected", this).text();

。并在 PHP 比较中使用正确的索引。