【问题标题】:Ajax call not working with jquery uiAjax 调用不适用于 jquery ui
【发布时间】:2011-09-04 10:29:33
【问题描述】:

使用 jquery ui 可排序,当发生更改时,将执行函数“位置”。它有一个 ajax 帖子,但在触发位置时不会发送:

$('#col').Sortable(
                    {
                        accept: 'widget',
                        opacity: 0.5,
                        helperclass: 'helper',
                        change:  positions,
                        handle: '.widget_title_bar'
                    }
                );

但是,“位置”确实有效,因为当我通过 onclick 操作正常调用它时,会发送 ajax 帖子。我唯一能想到的是职位是用javascript而不是jquery编写的。我该如何解决这个问题?

编辑我已包括以下职位:

function positions(){
    var widgets = '';
    var column = document.getElementById('col');
    for(i = 0; i < col_1.childNodes.length; i++) {
        var str1 = col_1.childNodes[i].className;
        if(str1 && str1.match('widget')) widgets+='&c[1]['+i+']='+col_1.childNodes[i].id;
    }

     xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
xmlhttp.open('POST', '/positions.php', true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("widgetpositions="+widgets);
xmlhttp.send(null);


    return true;
}

【问题讨论】:

  • 嗨 Dr.Molle,我已将“职位”功能编辑到我上面的原始帖子中。谢谢。
  • 函数里面的col_1是什么?
  • 抱歉,col_1 应该是 col。我编辑了一些代码以缩短 stackoverflow 的代码,但忘记更改了。
  • 如果您使用 jQuery UI,您必须使用 jQuery,然后您可能应该使用 jquery 中的一种 ajax 方法来让您的生活更轻松。

标签: jquery ajax jquery-ui jquery-ui-sortable


【解决方案1】:

Javascript标识符区分大小写,没有.Sortable()函数是jQuery UI,改用.sortable()

$('#col').sortable({
    accept: 'widget',
    opacity: 0.5,
    helperclass: 'helper',
    change:  positions,
    handle: '.widget_title_bar'
});

【讨论】:

    【解决方案2】:

    假设 sortable 中的大写 S 在缩短代码时是一个错字,这对我有用。
    错误一定在其他地方,请看演示:http://jsfiddle.net/doktormolle/xB8Wh/
    (我只用“更新”代替了“改变”,但它也适用于“改变”)

    从更改(或任何其他可排序事件)调用的唯一区别是范围。
    因此,如果您在函数内的某处使用 this-关键字,如果该函数由可排序事件调用,则它将是 #col,而从 onclick-event(assigned inline) 调用它this 将是全局窗口对象。

    那么你会在函数内部的某个地方使用 this 吗?

    【讨论】:

      猜你喜欢
      • 2011-04-04
      • 2013-01-17
      • 1970-01-01
      • 2015-01-16
      • 1970-01-01
      • 2018-04-14
      • 2019-07-06
      • 1970-01-01
      • 2018-07-23
      相关资源
      最近更新 更多