-1
我在使用Ajax的JQuery UI的自動完成插件時遇到問題。使用Ajax的Jquery UI自動完成開始於
它在本地工作,但它並不總是在服務器上工作,因爲控制檯給我「未捕獲的類型錯誤:無法讀取屬性'空'分裂'每當我只使用一個字符或根本沒有字符。它僅適用於2個字符...它確實很奇怪真的
這些都是我的文件
ajax.php:
<?php
require_once 'config.php';
if(!empty($_POST['type'])){
$type = $_POST['type'];
$name = $_POST['name_startsWith'];
$query = "SELECT id_pro, name, IFNULL(SUM(qty),0) as qty FROM inventory where ".$type." LIKE '%".$name."%' group by id_pro";
$result = mysqli_query($con, $query);
$data = array();
while ($row = mysqli_fetch_assoc($result)) {
$name = $row['id_pro'].'|'.$row['name'].'|'.$row['qty'];
array_push($data, $name);
}
echo json_encode($data);exit; }?>
auto.js
$(document).on('focus','.autocomplete_txt',function(){
type = $(this).data('type');
if(type =='id_pro')autoTypeNo=0;
if(type =='name')autoTypeNo=1;
$(this).autocomplete({
source: function(request, response) {
$.ajax({
url : 'ajax.php',
dataType: "json",
method: 'post',
data: {
name_startsWith: request.term,
type: type
},
success: function(data) {
response($.map(data, function(item) {
var code = item.split("|");
return {
label: code[autoTypeNo],
value: code[autoTypeNo],
data : item
};
}));
}
});
},
autoFocus: true,
minLength: 0,
select: function(event, ui) {
var names = ui.item.data.split("|");
id_arr = $(this).attr('id');
id = id_arr.split("_");
$('#itemNo_'+id[1]).val(names[0]);
$('#itemName_'+id[1]).val(names[1]);
$('#stock_'+id[1]).val(names[2]);
$('#quantity_'+id[1]).val(1);
}
}); });
despacho.php
<td><input class="case" type="checkbox"/></td>
<td><input type="text" data-type="id_pro" name="itemNo[]" id="itemNo_1" class="form-control autocomplete_txt changesNo" autocomplete="off" required></td>
<td><input type="text" data-type="name" name="itemName[]" id="itemName_1" class="form-control autocomplete_txt changesNo" autocomplete="off" required></td>
<td><input type="number" name="stock[]" id="stock_1" class="form-control changesNo" autocomplete="off" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;" readonly></td>
<td><input type="number" min="1" name="quantity[]" id="quantity_1" class="form-control changesNo" autocomplete="off" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;" required></td>