admin管理员组文章数量:1303496
I have many UL with li. Different UL will have li with the same id:
<ul>
<li id='1'>this</li>
<li id='2'>that</li>
</ul>
<ul>
<li id='1'>this</li>
</ul>
The user can drag li from on list to the other but only if the li with the same id does not exist. So far I have this:
$(".container").droppable({
drop: function (event, ui) {
var a = $(this).find("li").attr("id", $(ui.draggable).attr('id'));
if (a.length == 0) {
$.ajax({
...
});
}
}
});
However when I drop the draggable it changes the id of all li to the value of the dropped li.
Any guidance would be great. Thanks.
I have many UL with li. Different UL will have li with the same id:
<ul>
<li id='1'>this</li>
<li id='2'>that</li>
</ul>
<ul>
<li id='1'>this</li>
</ul>
The user can drag li from on list to the other but only if the li with the same id does not exist. So far I have this:
$(".container").droppable({
drop: function (event, ui) {
var a = $(this).find("li").attr("id", $(ui.draggable).attr('id'));
if (a.length == 0) {
$.ajax({
...
});
}
}
});
However when I drop the draggable it changes the id of all li to the value of the dropped li.
Any guidance would be great. Thanks.
Share Improve this question asked Apr 13, 2011 at 13:05 abarrabarr 1,1404 gold badges13 silver badges28 bronze badges2 Answers
Reset to default 7IDs cannot start with numbers and must be unique. For non-unique identifiers, use classes.
You can check to see if an element exists like this:
if($('#myDIV').length) {
}
By using attr()
, you're actually setting the id
attribute of any elements matched by your find()
call. You should use find
on the ID you're matching against.
$(".container").droppable({
drop: function (event, ui) {
var a = $(this).find("#" + $(ui.draggable).attr('id'));
if (a.length === 0) {
$.ajax({
...
});
}
}
});
Also, as @Diodeus mentions, IDs should not start with a number. They should also be unique document-wide, so if you need duplicate/expect IDs, I'd remend using a different attribute, such as an HTML5 data attribute: http://ejohn/blog/html-5-data-attributes/
本文标签: javascriptJquery check if li with idx exists in ul alreadyStack Overflow
版权声明:本文标题:javascript - Jquery check if li with id=x exists in ul already - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741759388a2396310.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论