0

リストが動的に作成されている場合、選択可能なリスト内のアイテムの「id」を取得するにはどうすればよいですか?

  <ul id="selectable">
  <li id='1'>..</li>
      .
      .
      <li...
  </ul>

試してみvar num = $('#selecable :selected').attr( "option" , 'id' );ましたが、[object Object] しか取得できません...

正しい方法は何ですか?

4

7 に答える 7

6

アップデート:

完全を期すために、要素が選択されている場合、プラグインはui-selected要素にクラスを追加します。したがって、次の方法で現在選択されている要素の ID を取得できます。

$('#selectable .ui-selected').attr('id');

ただし、複数の要素を選択できることに注意してください。


jQuery UI の選択可能なプラグインは、要素を選択するたびにコールバックを呼び出します。それを提供するだけです。

$("#selectable" ).selectable({
   selected: function(event, ui) { ... }
});

とはいえ、Nick が既に述べているように、ID は数字で始めることはできません。

:selectedoption要素でのみ機能します。

于 2010-05-24T19:17:43.240 に答える
5

このバージョンでは、特定のクラス名は必要ありません。$(ui.selected).attr('id')(最後に) 選択された要素の ID を取得するために使用します。

$( "#selectable" ).selectable({
    selected: function(event, ui) {
        alert( $(ui.selected).attr('id') );
    }
});
于 2011-11-21T01:58:46.100 に答える
4

選択された ID のリストは次のようになります: edit: better methinnks

$('#selectable').selectable(function(){
    selected:function(){ 
        var idlist = $(this).attr('id');
        //do something with the list...of id's
    }
});
于 2010-05-24T19:16:10.200 に答える
0
jQuery('#selectable').selectable(function(){
    selected: function(event, ui)
    {
     jQuery(".ui-selected", this).each(function() 
     {
          var objID=jQuery(this).attr("id");
     });
    }
});
于 2011-07-26T13:02:40.217 に答える
0

これをチェックして..

  $( ".ui-selected", this ).each(function() {
   var index = $( "#selectable li" ).attr(id);

    });
于 2014-01-07T12:29:47.743 に答える
0
jQuery('#selectable').selectable( function() {
    selected: function(event, ui)
    {
        alert(ui.selected.id);
    }
});
于 2012-07-05T14:19:22.000 に答える
-1
<script>
$(function () {
    $("#selectable").selectable({
        selected: function () {
            var id = $(".ui-selected", this).attr("id");
            //alert(id);
        }
    });
});

于 2014-06-27T23:43:33.907 に答える