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 C#读写EXCEL文件,不同编译
  • ¥15 如何提取csv文件中需要的列,将其整合为一篇完整文档,并进行jieba分词(语言-python)
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置