我正在尝试使用http://podio.github.com/jquery-mentions-input/将@mentions 功能添加到我的网站。我正在尝试使用 ajax 从查询数据库 onkeyup
的 .php 文件获取 JSON 响应,但我不知道在代码中的何处放置 ajax 调用。
我知道我要求人们基本上为我做这项工作,但我快死在这里了,我已经尝试了大约 2-3 天了
这里是插件中的两个 JavaScript 函数,我只是一个示例 ajax 函数,它将链接到我的 PHP 脚本,该脚本搜索用户 %LIKE%
查询。
插件的基本示例
$(function () {
$('textarea.mention').mentionsInput({
onDataRequest:function (mode, query, callback) {
var data = [
{ id:1, name:'Kenneth Auchenberg', 'avatar':'http://cdn0.4dots.com/i/customavatars/avatar7112_1.gif', 'type':'contact' },
{ id:2, name:'Jon Froda', 'avatar':'http://cdn0.4dots.com/i/customavatars/avatar7112_1.gif', 'type':'contact' },
{ id:3, name:'Anders Pollas', 'avatar':'http://cdn0.4dots.com/i/customavatars/avatar7112_1.gif', 'type':'contact' },
{ id:4, name:'Kasper Hulthin', 'avatar':'http://cdn0.4dots.com/i/customavatars/avatar7112_1.gif', 'type':'contact' },
{ id:5, name:'Andreas Haugstrup', 'avatar':'http://cdn0.4dots.com/i/customavatars/avatar7112_1.gif', 'type':'contact' },
{ id:6, name:'Pete Lacey', 'avatar':'http://cdn0.4dots.com/i/customavatars/avatar7112_1.gif', 'type':'contact' },
{ id:7, name:'<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d9b2bcb7b7bcadb199b8acbab1bcb7bbbcabbef7bdb2" rel="noreferrer noopener nofollow">[email protected]</a>', 'avatar':'http://cdn0.4dots.com/i/customavatars/avatar7112_1.gif', 'type':'contact' },
{ id:8, name:'Pete Awesome Lacey', 'avatar':'http://cdn0.4dots.com/i/customavatars/avatar7112_1.gif', 'type':'contact' },
{ id:9, name:'Kenneth Hulthin', 'avatar':'http://cdn0.4dots.com/i/customavatars/avatar7112_1.gif', 'type':'contact' }
];
data = _.filter(data, function(item) { return item.name.toLowerCase().indexOf(query.toLowerCase()) > -1 });
callback.call(this, data);
}
});
$('.get-syntax-text').click(function() {
$('textarea.mention').mentionsInput('val', function(text) {
alert(text);
});
});
$('.get-mentions').click(function() {
$('textarea.mention').mentionsInput('getMentions', function(data) {
alert(JSON.stringify(data));
});
}) ;
});
AJAX 示例(我不知道如何从 .php 文件获取 JSON)
$(function () {
$('textarea.mention-example2').mentionsInput({
onDataRequest:function (mode, query, callback) {
$.getJSON('assets/data.json', function(responseData) {
responseData = _.filter(responseData, function(item) { return item.name.toLowerCase().indexOf(query.toLowerCase()) > -1 });
callback.call(this, responseData);
});
}
});
});
最佳答案
我明白了,我只是添加了一个带有查询值的变量查询,并将其发送到我的脚本,然后该脚本搜索数据库并发回结果
$('textarea.mention-example2').mentionsInput({
onDataRequest:function (mode, query, callback) {
var myquery = 'query='+query;
$.getJSON('data.php', myquery, function(responseData) {
responseData = _.filter(responseData, function(item) { return item.name.toLowerCase().indexOf(query.toLowerCase()) > -1 });
callback.call(this, responseData);
});
}
});
关于php - jquery @mentions 输入 ajax 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14869501/