jquery 可排序连接列表的帮助

发布于 2024-10-30 10:49:21 字数 946 浏览 3 评论 0原文

这是我的代码的精确副本: jsFiddleCode

如您所见,我有两个可排序的连接列表和当某些项目被放入它们时,它们分别执行函数 subFunctionunsubFunction。现在,我还有双击其中一个项目的代码,以便将它们放入相反的列表中(函数 switchLists() 负责这一点。

现在,我想在这里完成什么与拖放项目时的行为相同(出现警报框并准确地说(例如):“项目 6 刚刚 subed”。

我缺乏理解,我怎么可能有 ui< /strong> 在调用函数 subFunction 时可用,而不是在调用 switchLists 时可用(我确实尝试将 ui 添加到调用中。 switchLists 像这样:

switchLists(e, ui){  
  //same code as before...

  //this code doesn't execute
  var itemText= ui.item.text();
  alert(itemText + " just subed");  
}

但是我在 Firefox 中的 FireBug 中收到一个错误,指出 ui 未定义。

您可以自由地在 fiddle 上编辑代码并将其

作为更一般的问题 发布到此处。 :jquery 如何将变量传递给其他函数?我的意思是,代码:

receive: subFunction  

在没有任何参数的情况下调用,那么 subFunction 如何获取事件和 ui?如果您有一些关于这一切的好教程,我们将不胜感激。

感谢您的帮助。

Here is the exact copy of my code: jsFiddleCode

As you can see I have a two sortable connected lists and when some item is dropped to them they execute functions subFunction and unsubFunction respectively. Now, I also have the code for doubleclicking one of the items so that then they are put in the opposite list (the function switchLists() takes care of that.

Now, what I would like to accomplish here is the same behavior as when the items are dragged and dropped (the alert box appearing and saying exactly (for example): "Item 6 just subed".

My lack of understanding is how can it be that I have the ui available when the function subFunction is called, and not when I call the switchLists. ( I did try to add ui to the call of switchLists like this:

switchLists(e, ui){  
  //same code as before...

  //this code doesn't execute
  var itemText= ui.item.text();
  alert(itemText + " just subed");  
}

But I get an error in FireBug in Firefox saying that the ui is undefined.

You are free to edit the code on fiddle and post it here as a link.

As a more general questions: how does jquery pass variables to other functions? I mean, the code:

receive: subFunction  

is called without any arguments, so how does the subFunction get event and ui? If you have some good tutorial on all this, it's appreciated.

Thank you for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

那一片橙海, 2024-11-06 10:49:21

经过一整天的研究,我终于找到了答案,并这样做了: jsFiddle 链接

简而言之,我将前面的函数分成了两个函数,并且我还阅读了更多有关 jQuery 的内容,发现在函数中我可以执行 $(this) 操作,从而访问元素的文本。

好的,仅供参考,整个代码在这里:

$(function() {
    $( "#sortable1" ).sortable({
        connectWith: ".connectedSortable",          
        receive: subFunction
    });     

    $( "#sortable2" ).sortable({
        connectWith: ".connectedSortable",          
        receive: unsubFunction
    });     

    $(".ui-state-default").dblclick(function() {
        $(this).removeClass().addClass("ui-state-highlight");           

        var litem = $(this).clone();
        litem.appendTo($('#sortable2'));
        $(this).remove();

        $.jGrowl($(this).text() + " successfully unsubed!", {header:"Subscription Status", life: 1000});
    });

    $(".ui-state-highlight").dblclick(function() {
        $(this).removeClass().addClass("ui-state-default");         

        var litem = $(this).clone();
        litem.appendTo($('#sortable1'));
        $(this).remove();

        $.jGrowl($(this).text() + " successfully subed!", {header:"Subscription Status", life: 1000});          
    });     

    function subFunction(event, ui) {
        ui.item.toggleClass("ui-state-default");
        ui.item.toggleClass("ui-state-highlight");

        $.jGrowl(ui.item.text() + " successfully subed!", {header:"Subscription Status", life: 1000});
    }

    function unsubFunction(event, ui) {
        ui.item.toggleClass("ui-state-default");
        ui.item.toggleClass("ui-state-highlight");

        $.jGrowl(ui.item.text() + " successfully unsubed!", {header:"Subscription Status", life: 1000});
    }

});

After a long day playing with this I finally came to the answer and did it like this: jsFiddle link

In short, I separated the previous function to two functions and I also read a little more about jQuery and found out that in the function I can do $(this) and so access the text of the element.

OK, just for reference the whole code is here:

$(function() {
    $( "#sortable1" ).sortable({
        connectWith: ".connectedSortable",          
        receive: subFunction
    });     

    $( "#sortable2" ).sortable({
        connectWith: ".connectedSortable",          
        receive: unsubFunction
    });     

    $(".ui-state-default").dblclick(function() {
        $(this).removeClass().addClass("ui-state-highlight");           

        var litem = $(this).clone();
        litem.appendTo($('#sortable2'));
        $(this).remove();

        $.jGrowl($(this).text() + " successfully unsubed!", {header:"Subscription Status", life: 1000});
    });

    $(".ui-state-highlight").dblclick(function() {
        $(this).removeClass().addClass("ui-state-default");         

        var litem = $(this).clone();
        litem.appendTo($('#sortable1'));
        $(this).remove();

        $.jGrowl($(this).text() + " successfully subed!", {header:"Subscription Status", life: 1000});          
    });     

    function subFunction(event, ui) {
        ui.item.toggleClass("ui-state-default");
        ui.item.toggleClass("ui-state-highlight");

        $.jGrowl(ui.item.text() + " successfully subed!", {header:"Subscription Status", life: 1000});
    }

    function unsubFunction(event, ui) {
        ui.item.toggleClass("ui-state-default");
        ui.item.toggleClass("ui-state-highlight");

        $.jGrowl(ui.item.text() + " successfully unsubed!", {header:"Subscription Status", life: 1000});
    }

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