我希望将输入字段的id传递给提供选项的PHP文件。这是我的HTML & jQuery代码。但是PHP程序获得的id是undefined。谢谢你的帮助。
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);
}
}); // autocompleteHTML:
<input type="text" class="classfield" id="hello" value="there"></input><br>发布于 2012-03-22 15:17:09
尝试从this.element获取id
//pass request to server
$.getJSON("ajax/ajax_suggestions.php?id="+this.element.attr('id')+"&callback=?", req, function(data) {另请参阅this example。
发布于 2012-03-22 15:07:29
$(this).attr('id')的值未定义,因为this是放入autocomplete参数中的对象( autocomplete的参数接受一个对象,因此如果使用$(this).attr('id'),则引用的是在autocomplete的参数中传递的对象)
因此,您不能使用$(this).attr('id')。
你必须存储文本字段的id,可能是一个全局变量...希望这对你有一点帮助
https://stackoverflow.com/questions/9817735
复制相似问题