jQuery UI 可拖动 & AJAX Post 到控制器操作 MVC.NET 后的可删除中断
我在使此功能正常工作时遇到问题。
情况是:
- 用户将项目拖到 div 上。
- 在放置时,AJAX Post 会被发送到具有项目 ID 的控制器操作。
- 成功后,我用从数据库检索到的 html 标记替换该项目被拖放到的 div。 .replaceWith(data) 注意:此标记是一个 div,其 ID 与项目拖放到的 ID 相同,其中包含一些附加元素。
- 这会加载更新后的 div。
所有这些都完美地工作,几乎,问题是我无法将不同的项目拖到 div 上。我确信这与 AJAX Post 之后加载的新 div(具有相同 ID)有关。
这是我的 jQuery 代码
$(function () {
$('.draggable').draggable({ revert: true });
$('#layoutHeader').droppable({
drop: function (event, ui) {
$.ajax({
type: "POST",
url: '/SiteSetup/GetMarkup/' + $(ui.draggable).attr("id"),
success: function (data) {
$('.draggable').draggable('destroy');
$('#layoutHeader').replaceWith(data);
$('.draggable').draggable({ revert: true });
}
});
}
});
});
I'm having a problem getting this functionality to work.
Situation is:
- User Drags an item to a div
- On the drop an AJAX Post is sent to a controller action with the item ID
- On success I replace the div the item was dropped onto with html markup retrieved from the database. .replaceWith(data) Note: This markup is a div with the same ID as that which the item was dropped onto with some additional elements contained within.
- This loads the updated div.
All this works perfectly, well almost, the problem is that I cannot drag a different item onto the div. I'm sure it has something to do with the new div (with the same ID) being loaded in after the AJAX Post.
Here's my jQuery code
$(function () {
$('.draggable').draggable({ revert: true });
$('#layoutHeader').droppable({
drop: function (event, ui) {
$.ajax({
type: "POST",
url: '/SiteSetup/GetMarkup/' + $(ui.draggable).attr("id"),
success: function (data) {
$('.draggable').draggable('destroy');
$('#layoutHeader').replaceWith(data);
$('.draggable').draggable({ revert: true });
}
});
}
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 jQuery 和 DOM,如果您用完全相同的 DOM id 替换 DOM 元素(因此 id 为“div1”的 div 被另一个 id 为“div1”的 div 替换),则关联的 jQuery 数据将与该元素相关联。 DOM 元素丢失。 jQuery 的指针实际上指向页面上不再存在的幽灵元素。
要查看证据,请在 Firefox 中启动 firebug,然后简单地查看
AJAX 帖子之前和之后。两次单击控制台中返回的元素。您会注意到,第二次单击(即 POST 返回后的一次)您的萤火虫将指向一个灰色的幽灵元素。
解决方案是稍微重构一下你的代码。试试这个
With jQuery and DOM, if you replace a DOM element with exactly the same DOM element with exactly the same DOM id ( so a div with id "div1" gets replaced by another div with id "div1") then the associated jQuery data with that DOM element gets lost. jQuery's pointer actually points to a ghost element which no longer exist on the page.
To see the proof, fire up firebug in Firefox and simply go
before and after your AJAX post. Click on the element returned in the console both the times. You will notice that on the second click (i.e. one after the POST returns) your firebug will point to a greyed out ghost element.
Solution is to refactor your code a bit. Try this
保留要插入 DOM 的 HTML 的
id
不会保留附加到您的div
的droppable
小部件正在更换。您需要重组代码,以便在新内容上重新初始化
droppable
小部件:或者,您可以用另一个
div< 包装
#layoutHeader
/code> 充当可放置
。这样,替换#layoutHeader
不应删除droppable
功能。Keeping the
id
of the HTML you're inserting into the DOM isn't going to preserve thedroppable
widget that was attached to thediv
you're replacing.You'll need to restructure your code so that the
droppable
widget is re-initialized on the new content:Alternatively, you could wrap
#layoutHeader
with anotherdiv
that acts as thedroppable
. That way, replacing#layoutHeader
shouldn't remove thedroppable
functionality.