我正在使用输入框从ajax自动完成的数据库中选择项目。我想删除所选项目,以便不能再次选择它。我的代码如下:
HTML
<input id='inpSelectRecipient' type='text' class='form-control' placeholder='Type recipient name here' autocomplete='off'></input>
的Javascript
$("#inpSelectRecipient").autocomplete({
// Min Lenght Function
minLength: 2,
// Source Function
source: function (request, response) {
// Ajax Call
$.ajax({
url: '../getautocomplete.php',
data: request,
dataType: 'json',
type: 'GET',
success: function (data) {
// Ajax Data
response(data);
// Check if Results Exists
if (data.length === 0) {
// Alert Message/Class
varModalMessage = 'No Results Found'; // Message Text
varAlertClass = '4'; // Error Class
// Alert Message
modalAlert(varModalMessage, varAlertClass);
}
}
});
},
// Search Function
search: function(event, ui) {
},
// Select Function
select: function(event, ui) {
// Variable
var removedRecipient;
// Create Message Recipients
createMessageRecipients(ui.item.id);
// Clear Input
$('#inpSelectRecipient').val('');
// Test
//console.log('Receipients Array: ' + recipientsArray + ' Selected Array: ' + removedRecipient);
},
// Response Function
response: function( event, ui ) {
}
});
PHP
<?php
// Obtain Term
$term=$_GET["q"];
// Query
$query=mysql_query("SELECT * FROM tblUsers WHERE `userFirstName` LIKE '%" . $term . "%' OR `userLastName` LIKE '%" . $term . "%' ORDER BY userFirstName ");
// Json Array
$json=array();
// Array Function
while($contact=mysql_fetch_array($query)){
// JSON Array
$json[]=array(
'id'=>$contact["userID"],
'value'=>$contact["userFirstName"]." ".$contact["userLastName"],
'label'=>$contact["userFirstName"]." ".$contact["userLastName"]
);
}
// Obtain Json
$functionReturn = json_encode($json);
// Echo Function Return
echo json_encode($json);
?>
我见过其他例子,但没有人使用ajax来获取自动完成数组。非常感谢任何帮助:)
答案 0 :(得分:0)
*you need to send selected items id to server so that in sql query it can exclude that result.
var selectedElm = [];
$("#inpSelectRecipient").autocomplete({
......
});
select: function(event, ui) {
.....
selectedElm.push(ui.item.id);
....
}
source: function (request, response) {
request.exclude = selectedElm ;
$.ajax({
....
data: request,
.....
}
and on server side just use "where not in" query.*