Javascript jqueryui:在drag'中获取源元素的ID;n';兴奋剂
我正在推动一个drag'n'drop,您可以将用户拖动到一个角色。我知道如何获取用户ID和目标角色ID,但我不知道如何获取从何处拖动用户的角色IDJavascript jqueryui:在drag'中获取源元素的ID;n';兴奋剂,javascript,jquery,jquery-ui,jquery-ui-draggable,jquery-ui-droppable,Javascript,Jquery,Jquery Ui,Jquery Ui Draggable,Jquery Ui Droppable,我正在推动一个drag'n'drop,您可以将用户拖动到一个角色。我知道如何获取用户ID和目标角色ID,但我不知道如何获取从何处拖动用户的角色ID <div id="role_1" class="role"> <h5>Administrator</h5> <ul class="users"> <li id="user_1">Foo</li> <li id="user_2"
<div id="role_1" class="role">
<h5>Administrator</h5>
<ul class="users">
<li id="user_1">Foo</li>
<li id="user_2">Bar</li>
</ul>
</div>
<div id="role_2" class="role">
<h5>Member</h5>
<ul class="users">
<li id="user_1337">Baz</li>
</ul>
</div>
<script type="text/javascript">
$(function() {
// Get roles and users lists
var $templates = $(".role"),
$users = $(".users");
// let the user items be draggable
$("li", $users).draggable({
revert: "invalid", // when not dropped, the item will revert back to its initial position
containment: "document",
helper: "clone",
cursor: "move"
});
// let the roles be droppable, accepting the user items
$templates.droppable({
accept: ".users > li",
activeClass: "ui-state-highlight",
drop: function(event, ui) {
var $uid = ui.draggable.attr("id"),
$targetRid = $(this).attr("id"),
$sourceRid = ???;
// snip
}
});
});
</script>
管理员
Foo
- 栏
成员
Baz
$(函数(){
//获取角色和用户列表
var$templates=$(“.role”),
$users=$(“.users”);
//让用户项可以拖动
$(“li”,$用户)。可拖动({
revert:“invalid”//未删除时,项目将恢复到其初始位置
遏制:“文件”,
助手:“克隆”,
光标:“移动”
});
//让角色可以拖放,接受用户项
$templates.droppable({
接受:“.users>li”,
activeClass:“ui状态突出显示”,
drop:函数(事件、用户界面){
var$uid=ui.draggable.attr(“id”),
$targetId=$(this.attr(“id”),
$sourceRid=???;
//剪断
}
});
});
提前感谢您的帮助。钩住,并获得最近的。角色
:
$("li", $users).draggable({
revert: "invalid", // when not dropped, the item will revert back to its initial position
containment: "document",
helper: "clone",
cursor: "move",
start: function() {
var role = $(this).closest(".role").attr("id");
// Here, role is either the id or undefined if no role could be found
}
});
如果在删除该信息时需要该信息,可以在开始事件中将其存储在元素上,然后在删除时检索该信息。钩住,并获取最近的。角色:
$("li", $users).draggable({
revert: "invalid", // when not dropped, the item will revert back to its initial position
containment: "document",
helper: "clone",
cursor: "move",
start: function() {
var role = $(this).closest(".role").attr("id");
// Here, role is either the id or undefined if no role could be found
}
});
如果在拖放时需要该信息,可以在开始事件中将其存储在元素上,然后在拖放时检索该信息。我认为在启动拖动事件时需要记住此id:
var srcId;
$("li", $users).draggable({
revert: "invalid", // when not dropped, the item will revert back to its initial position
containment: "document",
helper: "clone",
cursor: "move",
start: function( event, ui ) {
srcId = $(this).closest(".role").attr('id');
}
});
我认为启动拖动事件时需要记住此id:
var srcId;
$("li", $users).draggable({
revert: "invalid", // when not dropped, the item will revert back to its initial position
containment: "document",
helper: "clone",
cursor: "move",
start: function( event, ui ) {
srcId = $(this).closest(".role").attr('id');
}
});
谢谢你给了我需要的提示。。。我刚刚在可拖放的drop事件中执行了ui.draggable.closest(.role”).attr(“id”)
)@Hikaru Shindo:啊,是的,当然。不错!因为这与我的答案大不相同,你可能想把它作为你自己的答案发布,然后(在两天内)接受它。在这里很好,就这样。当然是你的电话。无论如何,很高兴能帮上忙@T.J.Crowder,我可以请你在这里看一个jQuery可拖放的相关问题吗:?谢谢你给了我我需要的提示。。。我刚刚在可拖放的drop事件中执行了ui.draggable.closest(.role”).attr(“id”)
)@Hikaru Shindo:啊,是的,当然。不错!因为这与我的答案大不相同,你可能想把它作为你自己的答案发布,然后(在两天内)接受它。在这里很好,就这样。当然是你的电话。无论如何,很高兴能帮上忙@T.J.Crowder,我可以请您在这里看一个jQuery可拖放的相关问题吗?