Jquery脚本无法正常工作

时间:2014-02-24 23:56:20

标签: javascript jquery

我这里有自动完成脚本,不允许不在选项列表中的值。但问题是脚本不起作用。当我尝试输入时,唯一出现在文本框中的是[]。为什么我的脚本无法正常工作?

主页中的脚本

<script>
$(function() {
    $("#autocomplete").autocomplete('autocomplete.php?', { mustMatch: true });
});

function changeAutoComplete (val) {
    $( "#autocomplete" ).autocomplete({
        source: 'autocomplete.php?selected='+val
    });
}

$('input#autocomplete').result(function(event, data, formatted) {
    //$("#result").html(!data ? "No match found!" : "Selected: " + formatted);
}).keyup(function() {
    $(this).search();
    $(this).css("background-color", "#D6D6FF");
});
</script>

主页中的Html

Drop1
 <?php
    $mysqli = new mysqli("localhost", "root", "", "2015");
    $combo = $mysqli->query("SELECT * FROM category GROUP BY cat_code ORDER BY id");
    $option = '';
     while($row = $combo->fetch_assoc())
        {
        $option .= '<option value = "'.$row['cat_code'].'">'.$row['category'].'</option>';
        }
    ?>
<select id="main" name="main" onchange="changeAutoComplete(this.value)">
<option value=""  selected="selected">Choose</option>
<?php echo $option; ?>
</select>
<input id="autocomplete" />

这是我在 autocomplete.php

中的ajax
$mysqli = new mysqli("localhost", "root", "", "2015") or die("Database Error");
$auto = $mysqli->real_escape_string($_GET["term"]);
$selected = $mysqli->real_escape_string($_GET["selected"]);
$sql = $mysqli->query("SELECT * FROM code WHERE item LIKE '%$auto%' AND cat_code='$selected' GROUP BY id ORDER BY item" ); 

if($sql)
{
    $str = array();
    while($row=mysqli_fetch_array($sql))
    {
        $str[] = $row['item'];
    }
    echo json_encode($str);
}

0 个答案:

没有答案