本文介绍了jQuery UI可选择获取ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果列表是动态创建的,如何获得可选列表中项目的"id"?

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>

我尝试了 var num = $('#selecable:selected').attr("option",'id'); ,但只获得了[object Object] ...

I tried var num = $('#selecable :selected').attr( "option" , 'id' ); but get only [object Object]...

正确的方法是什么?

推荐答案

更新:

出于完整性考虑,如果选择了元素,则插件会向该元素添加 selected 类.因此,您可以通过以下方式获取当前所选元素的ID:

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.

jquery ui可选插件在选择元素时调用回调,您只需提供它即可:

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) { ... }
});

也就是说,就像尼克已经提到的那样,ID不能以数字开头.

That said, as Nick already mentions, IDs cannot start with a digit.

:selected 仅适用于 option 元素.

这篇关于jQuery UI可选择获取ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 14:24