weixin_33739646 2015-03-10 21:06 采纳率: 0%
浏览 27

自动完成搜索框

I followed a video tutorial but I failed to output the result regardless many hours works. The output I expect is when user type some name to search for a company for example, companies' name will be show as a suggestion list. After select a certain company, more details of the company will be shown such as location, opening hours. I attach my HTML, JavaSCript and example JSON file here.

<body>
    <div id="searcharea">
        <label for="search">Ajax search</label>
        <p> enter the name </p>
        <input type="search" name="search" id="search" placeholder="company 
         name" />
    </div>

<script 
   src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"> 
 </script> 
<script src="script.js"></script>
</body>
</html>

$(#search).keyup(function() {
    var searchFiled = $('#search').val();
    var myExp = new RegExp(searchField, "i");

    $.getJSON('mylist.json', function(data) {

        var output = '<ul class="searchresults">';
        $.each(data, function(key, val) {
            if (val.user.search(myExp) != -1) {
                output += '<li>';
                output += '<h2>' + val.company + '</h2>';
                output += '</li>';
            }
        });
        output += '</ul>';
        $('$update').html(output);

    });
});    

{
    "data": [{

        "user_id": "1",
        "name": "Lala",
        "address": "somewhere on the world",
        "company": "big company",

    }, {
        "user_id": "2",
        "name": "Tom",
        "address": "USA",
        "company": "CocaCola",

    }]
    "reminds": 0,
    "message": "this is a message",
    "myID": 0
}
  • 写回答

0条回答 默认 最新

    报告相同问题?