简体   繁体   English

由html5sortable的sortupdate触发的JavaScript被运行两次

[英]JavaScript triggered by html5sortable's sortupdate is being run twice

On an aspx page I have a sortable list like this that is made using this plugin. 在aspx页面上,我有一个使用此插件制作的可排序列表

<ul id="sortableQueue" class="sortable">
    <li style="cursor: move" data-shotid="1">Shot Name - 1</li>
    <li style="cursor: move" data-shotid="2">Shot Name - 2</li>
    <li style="cursor: move" data-shotid="3">Shot Name - 3</li>
</ul>

Then at the bottom of the page I have code that loads the js for the sortable list and sets up a function called "sortupdate" which is run each time the order of the list changes. 然后在页面底部,我有代码为可排序列表加载js,并设置了一个名为“ sortupdate”的函数,该函数在列表顺序每次更改时运行。

<script src="js/html.sortable.js"></script>
<script>
    $('.sortable').sortable().bind('sortupdate', function (e, ui) {
        alert(ui.oldindex + ' -> ' + ui.item.index());
    });
</script>

Here's where things get bizarre. 事情变得奇怪了。 Upon moving an item from one place to another, such as item 1 to item 2's spot, the alert works as expected, returning "0 -> 1" BUT then another alert box shows up with the exact same "0 -> 1". 将一个项目从一个地方移动到另一个地方(例如,将项目1移到项目2的位置)时,警报将按预期工作,并返回“ 0-> 1”,但另一个警报框将显示为完全相同的“ 0-> 1”。 It seems to me that the Javascript is running twice for some reason, but I've never seen anything like this before, and have no idea what could be causing it. 在我看来,Java出于某种原因运行了两次,但是我之前从未见过这样的事情,也不知道是什么原因引起的。

I need it to run only once, otherwise extremely strange things will happen when I try to store the new order. 我只需要运行一次,否则当我尝试存储新订单时会发生非常奇怪的事情。

Anyone seen a similar issue before, and have some advice? 有人以前见过类似的问题,并且有什么建议吗? I can't tell if it's a JavaScript problem, an ASP.NET problem, or a html5sortable problem, so I don't even know where to begin. 我不知道这是JavaScript问题,ASP.NET问题还是html5sortable问题,所以我什至不知道从哪里开始。

I just ran into this myself, and I think it's an artifact of the alert. 我本人只是遇到了这种情况,我认为这是警报的产物。 Try removing the alert, and replacing it with a console.log call instead, and view the log messages in the console tab of your browser's inspector. 尝试删除警报,然后用console.log调用代替,并在浏览器检查器的“控制台”选项卡中查看日志消息。

Just make sure you remove the console.log call when you release ... IE doesn't like it unless there is actually a console. 只需确保在释放时删除console.log调用即可。IE不会喜欢它,除非实际上有一个控制台。 You'll know that this is the problem if the page fails when you try and load it in regular IE, but when you open the console, it succeeds, so you can't find out where the problem was :-( 如果您尝试将页面加载到常规IE中时页面失败,那么您就会知道这是问题,但是当您打开控制台时,页面成功了,因此您无法找出问题所在:-(

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM