jquery ui selectable get id?

不羁的心 提交于 2021-02-18 19:37:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!