weixin_33712881 2017-11-10 17:44 采纳率: 0%
浏览 32

AJAX自动完成

I have an AJAX autocomplete form. After many issues it works. However I need help with three issues.

  1. If the user type and result display, if the user backspace, the results remain in schoollist. How do I clear schoollist searchbox if is empty.

  2. Some of the words contain letters like ë. When retrieved from the database it display a ■ instead of ë.

  3. If there is no results, it will display "School not found". If you click on school not found, it accepts the answer. I prevent clicking on "School not found?

HTML

<div class="ui-widget">
<label>What school does the child attend<input  type="text" name="school" id="school" class="form-control" placeholder="Enter school Name"/></label>
<div id="schoollist"></div>  
</div>

AJAX

   $(document).ready(function(){  
   $('#school').keyup(function(){  
       var query = $(this).val();  
       if(query != '')  
       {  
            $.ajax({  
                 url:"search.php",  
                 method:"POST",  
                 data:{query:query},  
                 success:function(data)  
                 {  
                      $('#schoollist').fadeIn();  
                      $('#schoollist').html(data);  
                 }  
            });  
       }  
  });  
  $(document).on('click', 'li', function(){  
       $('#school').val($(this).text());  
       $('#schoollist').fadeOut();  
  });  
 });  

PHP

if (isset($_GET['term'])){
$return_arr = array();

try {
    $conn = new PDO("mysql:host=".DB_SERVER.";port=8889;dbname=".DB_NAME, DB_USER, DB_PASSWORD);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $stmt = $conn->prepare('SELECT School FROM Schools WHERE School LIKE :term');
    $stmt->execute(array('term' => '%'.$_GET['term'].'%'));

    while($row = $stmt->fetch()) {
        $return_arr[] =  $row['School'];
    }

} catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}


/* Toss back results as json encoded array. */
echo json_encode($return_arr);
}

https://jsfiddle.net/47v1t3k4/1/

  • 写回答

2条回答 默认 最新

  • weixin_33721427 2017-11-10 18:06
    关注

    1- I think a simple empty before your AJAX call will solve the problem: $('#schoollist').empty();

    2- Use <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> In your html, and also try to set the content type header of your response to utf-8 Like this: header('Content-Type: text/html; charset=utf-8');

    3- To prevent click event if no result found you have to use off method:

    $('#school').keyup(function(){  
        var query = $(this).val().trim();  
        $('#schoollist').empty();
        if(query != '')  
        {  
            $.ajax({  
                url:"search.php",  
                method:"POST",  
                data:{query:query},  
                success:function(data)  
                {  
                    $('#schoollist').fadeIn();  
                    $('#schoollist').html(data);
                    if ( data.indexOf("School not found") > -1 ) {
                        // detach click event
                        $(document).off('click', 'li', go);
                    } else {
                        // attach click event  
                        $(document).on('click', 'li', go);
                    }
    
    
                }  
            });  
        }  
    });  
    
    function go(){  
        $('#school').val($(this).text());  
        $('#schoollist').fadeOut();  
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀