How do I get the 'id' of an item in a selectable list, if the list is created dynamically?
<ul id="selectable">
<li id='1'>..</li>
.
.
<li...
</ul>
I tried var num = $('#selecable :selected').attr( "option" , 'id' );
but get only [object Object]...
开发者_开发技巧What is the right way?
Update:
For completeness, if an element is selected, the plugin adds a class ui-selected
to the element. So you can get the ID of current selected element via:
$('#selectable .ui-selected').attr('id');
But be aware that multiple elements can be selected.
The jQuery UI selectable plugin calls a callback whenever you select an element, you just have to provide it:
$("#selectable" ).selectable({
selected: function(event, ui) { ... }
});
That said, as Nick already mentions, IDs cannot start with a digit.
:selected
only works on option
elements.
This version does not require specific class names. Use $(ui.selected).attr('id')
to get the (last) selected element's ID:
$( "#selectable" ).selectable({
selected: function(event, ui) {
alert( $(ui.selected).attr('id') );
}
});
The list of selected id's would be: edit: better methinnks
$('#selectable').selectable(function(){
selected:function(){
var idlist = $(this).attr('id');
//do something with the list...of id's
}
});
jQuery('#selectable').selectable(function(){
selected: function(event, ui)
{
jQuery(".ui-selected", this).each(function()
{
var objID=jQuery(this).attr("id");
});
}
});
jQuery('#selectable').selectable( function() {
selected: function(event, ui)
{
alert(ui.selected.id);
}
});
check this..
$( ".ui-selected", this ).each(function() {
var index = $( "#selectable li" ).attr(id);
});
<script>
$(function () {
$("#selectable").selectable({
selected: function () {
var id = $(".ui-selected", this).attr("id");
//alert(id);
}
});
});
精彩评论