问题
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?
回答1:
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.
回答2:
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') );
}
});
回答3:
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
}
});
回答4:
jQuery('#selectable').selectable(function(){
selected: function(event, ui)
{
jQuery(".ui-selected", this).each(function()
{
var objID=jQuery(this).attr("id");
});
}
});
回答5:
jQuery('#selectable').selectable( function() {
selected: function(event, ui)
{
alert(ui.selected.id);
}
});
回答6:
check this..
$( ".ui-selected", this ).each(function() {
var index = $( "#selectable li" ).attr(id);
});
回答7:
<script>
$(function () {
$("#selectable").selectable({
selected: function () {
var id = $(".ui-selected", this).attr("id");
//alert(id);
}
});
});
来源:https://stackoverflow.com/questions/2899540/jquery-ui-selectable-get-id