オプションを提供するPHPファイルにINPUTフィールドのIDを渡したいとします。ここに私のHTML & jQueryコードです。しかし、PHPプログラムは、undefined
というIDを取得します。助けてくれてありがとう。jQueryオートコンプリートのPHP補完プログラムにHTML INPUTフィールドIDを渡す
のjQuery:
$('.classfield').autocomplete({
//define callback to format results
source: function(req, add){
//pass request to server
$.getJSON("ajax/ajax_suggestions.php?id="+$(this).attr('id')+"&callback=?", req, function(data) {
//create array for response objects
var suggestions = [];
//process response
$.each(data, function(i, val){
suggestions.push(val.name);
});
//pass array to callback
add(suggestions);
});
},
//define select handler
change: function(e) {
$("#spill").html("change "+$(this).val()+e.type);
}
}); // autocomplete
HTML:this
は、オートコンプリートのパラメータに置かれているオブジェクトであるため、$(this).attr('id')
の
<input type="text" class="classfield" id="hello" value="there"></input><br>
あなたはHTML btwを追加しておらず、サーバーのURLに "...&callback =?"という奇妙な記号があります。 – shershen