「オートコンプリート」で結果が見つからない場合にボタンを表示する関数を作成しようとしています。jQueryオートコンプリートレスポンス:nullのプロパティ 'length'を読み取ることができません
PHP
echo json_encode($row_set);
JS
$('#objNr').autocomplete({
source: 'php/v.php',
minLength: 2,
response: function(event, ui) {
// ui.content is the array that's about to be sent to the response callback.
if (ui.content.length === 0) {
$('#newTableBtn').removeClass('hidden');
} else {
$("#newTableBtn").addClass('hidden');
}
},
error: function(err){
console.log(err);
}
});
検索がうまく動作します。私は結果を得る。しかし、私が結果が出なかったら、私はこのメッセージを受け取ります:
Uncaught TypeError: Cannot read property 'length' of null at HTMLInputElement.response
何が問題なのですか?
UPDATE PHP
while ($row = $stmt->fetch()){
$row['id'] = $i+1;
$row_set[] = $row;//build an array
}
echo json_encode($row_set);//format the array into json data
UPDATE JS
response: function(event, ui) {
if (ui.content != null && ui.content.length < 1) {
$('#newTableBtn').removeClass('hidden');
console.log('if')
} else {
$("#newTableBtn").addClass('hidden');
console.log('else')
}
},
この1つはエラーメッセージをクリアします..しかし、それ以外の場合は、それが結果だかどうか消灯します...
'$ row_set'がどのように作成されているのがうれしいです。おそらく、正しいキーを持っていない、その変数の内容に関する単なる問題です。 –
@ErenorPaz done! –
'IF'の前に' console.log(ui) 'を置くと、あなたが実際にサーバから受け取っているものを知るべきです。変数の作成で問題が発生したとします。あなたは、行のJSON配列を持っていますが、その配列 –