jQuery可选获取文本值 [英] jQuery Selectable Get Text Value

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

问题描述

我有一个无序的项目列表-此列表可能长达数百个项目-当用户单击一个项目时,我会在选项的右侧显示其选择.我正在使用jQuery可选和可排序的组合来做到这一点(尽管单击选项后我失去了可排序的功能),并且为用户显示的值是所选项目的索引.我想为用户显示订单项的文本值,但是正如预期的那样,抓取text()值将返回所有订单项的文本值.

I have an unordered list of items - this list could be hundreds of items long - and as the user clicks an item, I display their selections to the right of the options. I'm using a combination of jQuery selectable and sortable to do so (although I lose the sortable once an option is clicked), and the value that is displayed for the user is the index of the selected item. I want to display the text value of the line item for the user instead, but as expected, grabbing the text() value returns the text values for all line items.

如何仅获取所选文本并显示给用户?

How can I grab just the selected text and display for the user?

HTML:

<ul id="list">
    <li id="Test1">Item 1</li>
    <li id="Test2">Item 2</li>
    <li id="Test3">Item 3</li>
    <li id="Test4">Item 4</li>
    <li id="Test5">Item 5</li>
</ul>

<p id="feedback">
<span>You've selected items:</span>    
    <ul id="select-result">
     <span id="select-resultSpan">
        <li>No Items Selected</li>
     </span>
    </ul>    
</p>

JS:

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

完整示例在JSFiddle上: http://jsfiddle.net/Jk6ZH/1/

Full example is on JSFiddle: http://jsfiddle.net/Jk6ZH/1/

谢谢.

推荐答案

如果我没看错,认为这就是您所需要的:

If I'm reading this right, think this is all you need:

$(".ui-selected", this).each(function () {

    // this works for me
    result.append("<li>" + $(this).text() + "</li>");
});

http://jsfiddle.net/JuJDt/

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆