我正在尝试做其他几个人在堆栈上已经完成的事情。我已经尝试了所有可用的示例,但似乎无法使其工作。我已经复制了工作示例并反映了与我的情况相匹配的更改,但仍然没有。
我使用的 HTML 看起来像这样。
<tr>
<td><a id="remRow"><span class="icon icon-squared-cross"></span></a></td>
<td><input type="hidden" data-type="itemID" name="itemID[]" id="itemID" class="form-control autocomplete_txt" autocomplete="off">
<input type="text" data-type="item_name" name="item_name[]" id="item_name" class="form-control autocomplete_txt" autocomplete="off" placeholder="Item Name"></td>
<td><input type="text" name="item_sku[]" id="item_sku" class="form-control changesNo" autocomplete="off" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;" placeholder="SKU#"></td>
<td><input type="text" name="item_qty[]" id="item_qty" class="form-control changesNo" autocomplete="off" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;" placeholder="Qty"></td>
<td><input type="text" name="item_rate[]" id="item_rate" class="form-control changesNo" autocomplete="off" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;" placeholder="Cost"></td>
<td><input type="text" name="balance[]" id="balance" class="form-control totalLinePrice" autocomplete="off" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;" placeholder="Balance"></td>
</tr>
Jquery 我已经从工作源得到了演示
//autocomplete script
$(".autocomplete_txt").keyup(function(){
type = $(this).data('type');
if(type =='itemID' )autoTypeNo=0;
if(type =='item_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("_");
element_id = id[id.length-1];
$('#itemID_'+element_id).val(names[0]);
$('#item_name_'+element_id).val(names[1]);
/*$('#quantity_'+element_id).val(1);
$('#price_'+element_id).val(names[2]);
$('#total_'+element_id).val( 1*names[2] );*/
calculateTotal();
}
});
});
最后,PHP 来处理 json。
case "fetchAll": {
$query = $db->rawQuery("SELECT itemID, item_name, item_sku FROM items ORDER BY item_name ASC");
if($query) {
$data = array();
foreach($query as $key => $val) {
//echo $val['itemID'];
$name = $val['itemID'].'|'.$val['item_name'].'|'.$val['item_sku'];
array_push($data, $name);
}
echo json_encode($out);
} else { echo "error"; }
}
break;
无论我使用什么示例,我都会不断收到未捕获的类型错误:无法读取未定义的属性“长度”。我尝试使用 jquery 3.0,并下载了最新的 jquery.ui,认为这可能是问题所在。我还尝试恢复到旧版本来检查这一点。
此时我确信我只是错过了一些东西。 3天有点荒谬,所以我寻求帮助。我知道堆栈上有类似的问题,是的,我已经尝试过所有这些问题。如果你还没准备好,猜猜我不太擅长 jquery。我可以让其他一切工作,甚至 ajax 调用,但是,这个......
Regards.