我使用jQuery UI排序可排序连接的列表.更新事件似乎运行两次.

这是完整的可排序的电话:

$(".pageContent").sortable({
    handle: ".quesText",connectWith: ".pageContent",containment: "section",start: function(e,ui){
        ui.placeholder.height(ui.item.height());
    },placeholder: "sortable-placeholder",opacity: 0.5,cursor: "move",cancel: "input,select,button,a,.openClose",update: function(e,ui){
        var thisItem = ui.item;
        var next = ui.item.next();
        var prev = ui.item.prev();

        var thisNumber = thisItem.find(".quesNumb");
        var nextNumber = next.find(".quesNumb");
        var prevNumber = prev.find(".quesNumb");

        var tn = parseInt(thisNumber.text());
        var nn = parseInt(nextNumber.text());
        var pn = parseInt(prevNumber.text());

        var quesNumbs = $(".quesNumb");

        var newItemId = thisItem.attr("id").replace(/_\d+$/,"_");

        //test if we are dragging top down
        if(ui.position.top > ui.originalPosition.top){
            quesNumbs.each(function(i){
                var thisVal = parseInt($(this).text());
                var grandparent = $(this).parent().parent();
                var grandId = grandparent.attr("id").replace(/_\d+$/,"_");
                if(thisVal > tn && (thisVal <= pn || thisVal <= (nn - 1))){
                    $(this).text(thisVal - 1 + ".");
                    grandparent.attr("id",grandId + (thisVal - 1));
                }
            });
            if(!isNaN(pn) || !isNaN(nn)){
                if(!isNaN(pn)){
                    //for some reason when there is a sender pn gets updated,so we check if sender exists
                    //only occurs sorting top down
                    if($.type(ui.sender) !== "null"){
                        var senderId = ui.sender.attr("id");
                        thisNumber.text(pn + 1 + ".");
                        thisItem.attr("id",senderId + "_" + (pn + 1));
                        alert(thisItem.attr("id"));
                    }
                    else {
                        thisNumber.text(pn + ".");
                        thisItem.attr("id",newItemId + pn);
                        alert(thisItem.attr("id"));
                    }
                }
                else {
                    thisNumber.text(nn - 1 + ".");
                }
            }
            else {
                   //something will happen here
            }
        }
        //otherwise we are dragging bottom up
        else {
            quesNumbs.each(function(i){
                var thisVal = parseInt($(this).text());
                if(thisVal < tn && (thisVal >= nn || thisVal >= (pn + 1))){
                    $(this).text(thisVal + 1 + ".");
                }
            });
            if(!isNaN(pn) || !isNaN(nn)){
                if(!isNaN(pn)){
                    thisNumber.text(pn + 1 + ".");
                }
                else {
                    thisNumber.text(nn + ".");
                }
            }
            else {
               //something will happen here
            }
        }
    }
});

这是运行两次的部分:

if($.type(ui.sender) !== "null"){
                    var senderId = ui.sender.attr("id");
                    thisNumber.text(pn + 1 + ".");
                    thisItem.attr("id",senderId + "_" + (pn + 1));
                    alert(thisItem.attr("id"));
                }
                else {
                    thisNumber.text(pn + ".");
                    thisItem.attr("id",newItemId + pn);
                    alert(thisItem.attr("id"));
                }

当排序保持在同一列表中时,我希望只能获得一个警报,因为ui.sender为空.当一个项目离开一个列表去转到另一个,那么ui.sender将不再是null.

问题是当我将一个项目移动到一个新的列表中时,我会收到2个警报消息.它更像是在更新功能运行一次后设置ui.sender,然后再次运行更新功能.显然这是不好的,因为我覆盖不应该被覆盖的数据.

如果是这样,我应该如何重写代码以避免覆盖数据?

编辑

我相信每次更改列表DOM时调用更新事件,而不仅仅是一般的DOM.因此,对于具有DOM更改的每个列表,运行更新.当我将一个项目移动到新列表中时,我正在更新两个列表.

所以我猜这个新问题是:如何重写这个代码,知道它会发射两次?是否组合接收和删除事件可以实现这一点?

解决方法

我对每个可排序的事件进行了一些调查.这是我的发现,按照它们发生的顺序列出:
$(".pageContent").sortable({
    start: function(e,ui){
        //Before all other events
        //Only occurs once each time sorting begins
    },activate: function(e,ui){
        //After the start event and before all other events
        //Occurs once for each connected list each time sorting begins
    },change: function(e,ui){
        //After start/active but before over/sort/out events
        //Occurs every time the item position changes
        //Does not occur when item is outside of a connected list
    },over: function(e,ui){
        //After change but before sort/out events
        //Occurs while the item is hovering over a connected list
    },sort: function(e,ui){
        //After over but before out event
        //Occurs during sorting
        //Does not matter if the item is outside of a connected list or not
    },out: function(e,ui){
        //This one is unique
        //After sort event before all drop/ending events unless **see NOTE
        //Occurs,only once,the moment an item leaves a connected list
        //NOTE: Occurs again when the item is dropped/sorting stops 
        //--> EVEN IF the item never left the list
        //--> Called just before the stop event but after all other ending events
    },beforeStop: function(e,ui){
        //Before all other ending events: update,remove,receive,deactivate,stop
        //Occurs only once at the last possible moment before sorting stops
    },remove: function(e,ui){
        //After beforeStop and before all other ending events
        //Occurs only once when an item is removed from a list
    },receive: function(e,ui){
        //After remove and before all other ending events
        //Occurs only once when an item is added to a list
    },ui){
        //After receive and before all other ending events
        //Occurs when the DOM changes for each connected list
        //This can fire twice because two lists can change (remove from one
        //list but add to another)
    },deactivate: function(e,ui){
        //After all other events but before out (kinda) and stop
        //Occurs once for each connected list each time sorting ends
    },stop: function(e,ui){
        //After all other events
        //Occurs only once when sorting ends
    }
});

解决我的问题我只是强制我的更新功能的内容只运行一次,将其包装在一个if else语句中,检查ui.sender.基本上它说如果ui.sender不存在,那么这是第一次通过更新功能,我们应该做这个功能,否则什么都不做.

为什么jquery sortable中的update事件似乎在测试ui.sender时运行两次的更多相关文章

  1. jquery点赞功能实现代码 点个赞吧!

    点赞功能很多地方都会出现,如何实现爱心点赞功能,这篇文章主要为大家详细介绍了jquery点赞功能实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  2. ios – 当pod的新版本可用时,“pod update”是否会覆盖我的代码更改?

    我的更改会被覆盖吗?你能描述为什么是或为什么不?关于如何做到这一点.

  3. 在IOS9中的Cordova应用程序使用JQuery / Javascript的window.history问题

    在两个测试用例中唯一改变的是Cordova.js.解决方法我看到这是几个星期前,但我会发布这个,以防其他人遇到它.听起来它可能与iOS9中的哈希更改生成的导航事件有关.如果是这样,可以将其添加到index.html以禁用哈希侦听:

  4. iOS 5上的jQuery事件

    解决方法在Apple开发论坛上由一个人回答:我需要在将元素添加到DOM之后才绑定(),如下所示:

  5. ios – 什么是Sprite Kit中的SKSpinLockSync以及如何解决它

    我收到一个带有以下堆栈跟踪的错误报告,我不知道问题是什么.我已经看到这样的建议,这可能是由于在纹理图集中有一个发射器的图像,或者是在添加的同一个运行循环中移除了一个发射器,但我认为这些都不会发生.这是一个零星的问题,我无法再创造它.我只在bug报告中看到它.我很乐意帮忙.编辑:我现在意识到我在几种不同的情况下得到SKSpinLockSync问题,并不总是与发射器有关.我认为,我经常使用发射器看到它

  6. ios – 符合通用协议的结构类型,其关联类型也是协议

    在结构中,我想…是的,您可以使用AnySorter类型在DataHandler中保留属性.例如,对于一个人为的例子,我们可以让sortedItems成为一个计算属性,利用AnySorter实例对存储的项目列表进行排序(当然,实际上我们不希望对每个调用进行重新排序,但是仅适用于此示例!

  7. Carthage使用解决部分Swift运行时的问题

    1、首先,简单的利用Carthagehttps://github.com/Carthage/Carthage要求进行简单测试使用,建立一个demo测试2、可能由于xcode版本原因在:类似我执行时出现的错误:Argo.framework是用旧的编译器编译的(估计下载的那个包是用Xcode7编译因此在工程文件目录下需要执行将所有需要的包下载下来(原本执行update操作会直接将运行好的framewo

  8. pod install 与 pod update的区别

    podinstall是安装Podfile中指定版本的第三方库。podupdate是安装最新的版本,如果之前已经安装了,但是有新版本,会更新版本。

  9. android – 如何使用Cursor Adapter刷新listView

    我使用CursorAdapter创建了一个ListView.现在我正在尝试更新ListView并将值刷新到ListView.但我无法弄明白.如何使用Loader或changeCursor()来刷新我的ListView下面是我设置CursorAdapter的代码://SucessFully在这里完成我的按钮onClick我正在将值更新到数据库中//SucessFully完成我的UpdateData方法:CursorDemo类任何帮助都得到赞赏……});解决方法如果CursorDemo扩展了CursorAda

  10. Android通知操作未调用待处理的服务意图

    我试图让我的通知有一个按钮,当点击它应该调用我的服务,这是控制音频播放.这是我的意图通知这是我服务的开始从通知中的操作调用时,永远不会触发日志.但该服务由应用程序中的按钮使用,并且工作正常.按以下命令调用日志.解决方法使用:代替:^^这用于从通知中启动活动.

随机推荐

  1. jquery-plugins – 是否可以使用猫头鹰旋转木马实现循环/无限轮播?

    我正在使用猫头鹰旋转木马,它的工作完美,除了它不支持循环/无限滚动.我没有搜索google和stackoverflow的想法,没有运气.有没有人在猫头鹰旋转木马上实现圆形/无限滚动?

  2. jQuery动态输入字段焦点

    我想使用以下jQuery向我的页面动态添加一个输入字段:在这样做之后,我希望输入字段具有闪烁的文本光标的焦点,所以我想在创建后立即输入.有人可以告诉我我该怎么办?

  3. jquery – 为什么$(window).height()这样错了?

    我试图获取当前浏览器的视口高度,使用但我得到的价值观太低了.当视口高度高达850px时,我从height()获取大约350或400像素的值.这是怎么回事?

  4. jquery – 如果在此div之外和其他draggables内部(使用无效和有效的还原选项),则可拖动恢复

    例如这样但是由于明显的原因,这不行.我可以说这个吗?

  5. 创建一个jQueryUI 1.8按钮菜单

    现在jQueryUI1.8已经出来了,我正在浏览更新,并且遇到了新的Buttonwidget,特别是SplitButtonwithadropdown的演示之一.这个演示似乎表明Buttonwidget可以在这里创建一个下拉菜单.作为讨论的问题,我想知道使用这个新的Button小部件来创建一个下拉菜单有什么方法.干杯.解决方法您必须在按钮下方列出一个列表,方式类似于此处为自动完成提供的演示:http

  6. 灰色divs使用JQuery

    我试图使用这个代码:为了淡出一大堆名为MySelectorDiv的div,唯一的是,它只会淡出第一个而不是所有的div,为什么呢?

  7. 使用jQuery动态插入到列表中

    我有两个订单列表在彼此旁边.当我从一个列表中选出一个节点时,我想按照字母顺序插入到另一个列表中.抓住的是我想要把一个元素放在另一个列表中,而不刷新整个列表.奇怪的是,当我插入到右边的列表中,它工作正常,但是当我插入到左边的列表中时,顺序永远不会出来.我也尝试将所有内容读入数组,并将其排序在一起,以防止children()方法没有按照显示顺序返回任何东西,但是我仍然得到相同的结果.这是我的jQuer

  8. 没有回应MediaWiki API使用jQuery

    我试图从维基百科获取一些内容作为JSON:但我没有回应.如果我粘贴到浏览器的地址栏,就像我得到预期的内容.怎么了?解决方法您需要通过添加&callback=?来触发具有$.getJSON()的JSONP行为?在querystring上,像这样:Youcantestithere.没有使用JSONP,你正在击中same-originpolicy,阻止XmlHttpRequest获取任何数据.

  9. jQuery Ajax请求每30秒

    我有这段代码,但是有些人在我的网站上的值可能会改变.我需要每30秒钟更新一次#financediv.这可以做吗解决方法您可以将代码放在单独的函数中,如下所示:然后每30秒建立一个定时器调用该函数:祝你好运!总结以上是DEVMAX为你收集整理的jQueryAjax请求每30秒全部内容。如果觉得DEVMAX网站内容还不错,欢迎将DEVMAX网站推荐给好友。

  10. jquery – keypress事件在IE和Chrome中不工作,但在FF工作

    任何想法为什么会这样发生?我通常认为Chrome会更加宽容代码?这是我的按键键.我错过了什么吗?右图();和leftimage();是应该工作的功能,因为我在其他地方使用这些功能谢谢您的帮助!

返回
顶部