jQuery可选获取文本值

我有一个无序的项目列表 – 这个列表可能长达数百个项目 – 当用户点击一个项目时,我会在选项右侧显示他们的选择。 我正在使用jQuery selectable和sortable的组合来执行此操作(尽管我在单击选项时丢失了可排序的选项),并且为用户显示的值是所选项的索引。 我希望显示用户的订单项的文本值,但正如预期的那样,抓取text()值会返回所有订单项的文本值。

如何只为用户抓取所选文本和显示?

HTML:

  • Item 1
  • Item 2
  • Item 3
  • Item 4
  • Item 5

You've selected items:

  • No Items Selected

JS:

 $(function () { $("#list").selectable({ stop: function () { var result = $("#select-resultSpan").empty(); $(".ui-selected", this).each(function () { var index = $("#list li").index(this); result.append("
  • " + (index + 1) + "
  • "); }); } }); });

    完整的例子是JSFiddle: http : //jsfiddle.net/Jk6ZH/1/

    提前致谢。

    如果我正确读到这一点,请认为这就是您所需要的:

     $(".ui-selected", this).each(function () { // this works for me result.append("
  • " + $(this).text() + "
  • "); });

    http://jsfiddle.net/JuJDt/

    试试这个:

     $("#list li").on("click",function() { $("#select-resultSpan").append($(this).text()); }) 
     You have to find innerHTML from index value. you can also get other attributes with this method. $(function () { $("#list").selectable({ stop: function () { var result = $("#select-resultSpan").empty(); $(".ui-selected", this).each(function () { var index = $("#list li").index(this); var text = $("#list li").get(index).innerHTML; // for id you can replace .innerHTML with .id result.append("
  • " + (text) + "
  • "); }); } }); });

    http://jsfiddle.net/Jk6ZH/42/