我正在尝试做其他人在堆栈上完成的事情。我已经尝试了所有可用的例子,似乎不能得到这个工作。我已经复制了工作示例,并反映了与我的情况相匹配的更改,但仍然一无所获。
我使用的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;
我不断得到Uncaught TypeError:不能读取属性'length'的undefined,无论我使用什么例子。我尝试使用jquery 3.0,并下载了最新的jquery。我想这可能就是问题所在。我还试着回退到旧版本来检查一下。
在这一点上,我确信我只是错过了一些东西。3天有点荒谬,所以我请求帮助。我知道stack上也有类似的问题,是的,我都试过了。如果你还没有准备好,我猜我不太擅长jquery。我可以让其他一切工作,甚至ajax调用,但是,这…问候。
CLHardman:
尝试在head部分使用以下脚本:
Html文件:
<head>
<script src='http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.2.min.js'></script>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script>
<script>
$( document ).ready(function() {
//autocomplete script
$(".autocomplete_txt").keyup(function(){
type = $(this).data('type');
if(type =='productCode' )autoTypeNo=0;
if(type =='productName' )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();
}
});
});
});
</script>
</head>
我无法复制你的场景,但似乎你有冲突与你的jquery脚本。