为什么我的 jQuery jEditable、就地编辑回调不起作用?
我正在使用 jQuery jEditable 就地编辑字段,但是当 JSON 函数返回并且我尝试从就地编辑回调返回一个值时,我得到的只是一闪而过
您可以在这里看到...
http://clareshilland.unknowndomain.co.uk/
点击Ctrl+L 登录...
用户名:stackoverflow
密码:jquery
虽然您可以在 /script.js 中看到脚本,但这里是主要代码摘录...
$('#menu li a').editable(edit_menu_item, { cssclass: 'editable' });
这是回调:
function edit_menu_item(value, settings) {
$.ajax({
type : "POST",
cache : false,
url : 'ajax/menu/edit-category.php',
dataType: 'json',
data : { 'id': this.id, 'value': value },
success : function(data) {
if (data.status == 'ok') {
alert('ok');
return data.title;
} else {
alert('n/ok');
return this.revert;
}
}});
}
JSON 代码在这里:ajax/menu.edit-category.php
就地编辑是位于菜单上,上面也有 jQuery 可排序功能。单击即可编辑。输入保存,它会存储数据,但不会在就地编辑字段上更新数据。
请帮助 stackoverflow,我已经为此工作了很长时间。
提前致谢!
I am using a jQuery jEditable edit-in-place field but when the JSON function returns and I try to return a value from my edit-in-place callback all I get is a flash of
You can see this here...
http://clareshilland.unknowndomain.co.uk/
Hit Ctrl+L to login...
Username: stackoverflow
Password: jquery
Although you can see the script in /script.js here is the main code exerpt...
$('#menu li a').editable(edit_menu_item, { cssclass: 'editable' });
Here is the callback:
function edit_menu_item(value, settings) {
$.ajax({
type : "POST",
cache : false,
url : 'ajax/menu/edit-category.php',
dataType: 'json',
data : { 'id': this.id, 'value': value },
success : function(data) {
if (data.status == 'ok') {
alert('ok');
return data.title;
} else {
alert('n/ok');
return this.revert;
}
}});
}
The JSON code is here: ajax/menu.edit-category.php
The edit-in-place is is on the menu which also has a jQuery sortable on it. Single click to edit. Enter to save, and it stores the data but does not update it on the edit-in-place field.
Please help stackoverflow, I have been working on this for a mega age.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从可编辑主页 (http://www.appelsiini.net/projects/jeditable):
edit_menu_item 不返回字符串 - 似乎您可以通过返回值来解决这个问题,然后如果您需要更新实际内容,请在 ajax 请求的成功回调中执行此操作。您可能还想添加一个错误回调,以防万一。
另一种可能是个坏主意的替代方案是将您的 ajax 调用标记为同步(async : false)并查看responseText,但这会锁定页面,直到您的请求完成。
From the editable homepage (http://www.appelsiini.net/projects/jeditable):
edit_menu_item doesn't return a string - it would seem that you can get around that by returning value, then if you need to update the actual contents, do so in the success callback of your ajax request. You might want to also add an error callback, just in case.
An alternative, which is probably a bad idea, would be to mark your ajax call as synchronous (async : false) and looking at the responseText, but that would lock up the page until your request completes.
从服务器端传递 json 对象,该对象将被 Javascript 视为文本。您必须使用
var myObject = JSON.parse(myJSONtext);
PHP 代码
Js 代码将其解析为 json 对象
Pass the json object from server side, which will be treated as text by Javascript. You have to parse that to json object using
var myObject = JSON.parse(myJSONtext);
PHP Code
Js Code
我遇到了同样的问题,因此我通过引入回调修改了 Jeditable 源代码,以便我可以从 AJAX“成功”处理程序内部返回字符串值。
之前的 Source Jeditable:
之后的 Source Jeditable:
在您的成功处理程序中:
I had the same problem and therefor I modified the Jeditable source code by introducing a callback so that I can return the string value from inside the AJAX "succes" handler.
Source Jeditable before:
Source Jeditable after:
In your succes handler: