我有选择和输入字段。输入中的值取决于选择值。当我选择选项中的选项并输入例如输入自动完成中的2时工作正常。当我更改所选值并在输入中输入相同的值时,例如等于2时出现问题。在这种情况下,自动完成功能不起作用。如果我键入不同于以前的值,一切都可以。有人可以帮助我吗?
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.1/
themes/base/minified/jquery-ui.min.css" type="text/css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.10.1/jquery-ui.min.js"></script>
</head>
<body>
<form autocomplete="off" action="">
Select project:
<select name="project" id="project">
</select>
<input autocomplete="off" id="build" type="text" />
</form>
</body>
<script>
$(function ()
{
//-----------------------------------------------------------------------
// 2) Send a http request with AJAX http://api.jquery.com/jQuery.ajax/
//-----------------------------------------------------------------------
$.ajax({
url: 'getprojects.php', //the script to call to get data
data: "", //you can insert url argumnets here to pass to api.php
//for example "id=5&parent=6"
dataType: 'json', //data format
success: function(data) //on recieve of reply
{
//--------------------------------------------------------------------
// 3) Update html content
//--------------------------------------------------------------------
for ( var i = 0; i < data.length; i++ ) {
$('#project').append($('<option>', {
value: data[i],
text: data[i]
}));
}
}
});
$("#build").keyup(function(){
var str=($( "#project option:selected" ).text());
$('#build').autocomplete({source:'getbuilds.php?q='+str, minLength:1});
});
$("#build").click(function(){
var str=($( "#project option:selected" ).text());
$('#build').autocomplete({source:'getbuilds.php?q='+str, minLength:1});
});
$("#project").click(function(){
$("#build").val('');
});
});
</script>
</html>
getprojects
$results = $db->query('SELECT distinct PROJECT FROM Builds');
while ($row = $results->fetchArray())
{
$data[] = array(
$row['PROJECT']
);
}
echo json_encode($data);
?>
getbuilds:
$db = new SQLite3('test.db');
$rs = $db->query("SELECT LABEL FROM Builds where PROJECT = '{$_REQUEST['q']}'
AND LABEL like '%{$_REQUEST['term']}%'");
$data = array();
while ($row = $rs->fetchArray())
{
$data[] = array(
'label' => $row['LABEL']
);
}
echo json_encode($data);
flush();
?>