为什么我的 jQuery jEditable、就地编辑回调不起作用?

发布于 2024-08-26 04:03:59 字数 1016 浏览 3 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

○闲身 2024-09-02 04:03:59

从可编辑主页 (http://www.appelsiini.net/projects/jeditable):

请注意,函数必须返回字符串。
通常是编辑的内容。这将
编辑后显示在页面上的是
完成。

edit_menu_item 不返回字符串 - 似乎您可以通过返回值来解决这个问题,然后如果您需要更新实际内容,请在 ajax 请求的成功回调中执行此操作。您可能还想添加一个错误回调,以防万一。

另一种可能是个坏主意的替代方案是将您的 ajax 调用标记为同步(async : false)并查看responseText,但这会锁定页面,直到您的请求完成。

From the editable homepage (http://www.appelsiini.net/projects/jeditable):

Note that function must return string.
Usually the edited content. This will
be displayed on page after editing is
done.

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.

栖迟 2024-09-02 04:03:59

从服务器端传递 json 对象,该对象将被 Javascript 视为文本。您必须使用 var myObject = JSON.parse(myJSONtext);

PHP 代码

$response = new stdClass();
$response->value = $value;
print_r(json_encode($response));

Js 代码将其解析为 json 对象

//(in Jeditable callback)
"callback": function( sValue, y ) {
    sValue = JSON.parse(sValue);
    //now you can access sValue.value
}

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

$response = new stdClass();
$response->value = $value;
print_r(json_encode($response));

Js Code

//(in Jeditable callback)
"callback": function( sValue, y ) {
    sValue = JSON.parse(sValue);
    //now you can access sValue.value
}
别忘他 2024-09-02 04:03:59

我遇到了同样的问题,因此我通过引入回调修改了 Jeditable 源代码,以便我可以从 AJAX“成功”处理程序内部返回字符串值。

之前的 Source Jeditable:

/* check if given target is function */
if ($.isFunction(settings.target)) {
  var str = settings.target.apply(self, [input.val(), settings]);
  $(self).html(str);
  self.editing = false;
} else {

之后的 Source Jeditable:

/* check if given target is function */
if ($.isFunction(settings.target)) {
        var edit = self;
        settings.target.apply(self, [input.val(), settings, function(str){
            $(edit).html(str);
            edit.editing = false;
        }
    ]);
} else {

在您的成功处理程序中:

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:

/* check if given target is function */
if ($.isFunction(settings.target)) {
  var str = settings.target.apply(self, [input.val(), settings]);
  $(self).html(str);
  self.editing = false;
} else {

Source Jeditable after:

/* check if given target is function */
if ($.isFunction(settings.target)) {
        var edit = self;
        settings.target.apply(self, [input.val(), settings, function(str){
            $(edit).html(str);
            edit.editing = false;
        }
    ]);
} else {

In your succes handler:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文