如何以编程方式选择jQuery可选列表中的第一项

时间:2012-02-09 01:24:00

标签: jquery

  

可能重复:
  jQuery-ui: How to programatically select selectables

有点时间试图让它发挥作用。

如何选择可选列表中的第一个(或任何事项)项目?理想情况下,我希望事件“停止”响应它,因为我有逻辑设置其他控件中的值。

谢谢你, 斯蒂芬

$("#favoriteFolderSelectCopy").selectable({
    create: function () {
         // select the first item in the list??

    },
    stop: function () {

        var ar = $(".ui-selected", this);

        if (typeof (ar) === 'undefined') {
            return false;
        }

        if (ar.length === 1) {

            var folderName = $('.ui-selected', this).html();
            var ownerName = $('.ui-selected', this).attr('data-owner');
            var glCode = $('.ui-selected', this).attr('data-glcode');

            $("#favMaintName").val(folderName).removeClass("defaultTextActive").focus();
            $("#favMaintOwner").val(ownerName);
            $(".ui-autocomplete-input").val(glCode);

            return true;
        }


        if (ar.length > 1) {

            //$("#favMaintName").val(ar[0]["data-id"]);
            return true;
        }

    }

});

http://jqueryui.com/demos/selectable/#serialize

<ul class="ui-selectable" id="favoriteFolderSelectCopy">
    <li data-owner="" data-glcode="" data-id="2976199074" class="ui-widget-content ui-selectee ui-selected"> </li>
    <li data-owner="" data-glcode="" data-id="2977088462" class="ui-widget-content ui-selectee">ATLAS TEST Share</li>
    <li data-owner="" data-glcode="" data-id="2976171424" class="ui-widget-content ui-selectee">Food-Canned Goods</li>
    <li data-owner="" data-glcode="" data-id="2976199547" class="ui-widget-content ui-selectee">Food-Frozen</li>
    <li data-owner="" data-glcode="" data-id="2976199546" class="ui-widget-content ui-selectee">Food-Meat</li>
    <li data-owner="" data-glcode="" data-id="2977213083" class="ui-widget-content ui-selectee">HDS CANADA</li>
    <li data-owner="" data-glcode="" data-id="2977213137" class="ui-widget-content ui-selectee">HDS CANADA2</li>
    <li data-owner="" data-glcode="" data-id="2976539509" class="ui-widget-content ui-selectee">Kirsten Test</li>
    <li data-owner="" data-glcode="" data-id="2976380310" class="ui-widget-content ui-selectee">Light Bulbs</li>
    <li data-owner="" data-glcode="" data-id="2976377957" class="ui-widget-content ui-selectee">Office Supplies</li>
    <li data-owner="" data-glcode="" data-id="2003130884" class="ui-widget-content ui-selectee">Rooms - Appliances</li>
    <li data-owner="" data-glcode="" data-id="1776652731" class="ui-widget-content ui-selectee">Rooms - Chemicals</li>
    <li data-owner="" data-glcode="" data-id="1752600210" class="ui-widget-content ui-selectee">Rooms - Guest Supplies</li>
    <li data-owner="" data-glcode="" data-id="2165588708" class="ui-widget-content ui-selectee">Rooms - In-Room Coffee</li>
    <li data-owner="" data-glcode="" data-id="1771586932" class="ui-widget-content ui-selectee">Rooms - Linens</li>
    <li data-owner="" data-glcode="" data-id="2003130835" class="ui-widget-content ui-selectee">Rooms - Uniforms</li>
    <li data-owner="" data-glcode="" data-id="2977213478" class="ui-widget-content ui-selectee">SYSCO punchout test</li>
    <li data-owner="" data-glcode="" data-id="2976380420" class="ui-widget-content ui-selectee">Steve's Stuff</li>
    <li data-owner="" data-glcode="" data-id="2976377950" class="ui-widget-content ui-selectee">USFI</li>
    <li data-owner="" data-glcode="" data-id="2976490556" class="ui-widget-content ui-selectee">walk IN #1</li>
</ul> 

1 个答案:

答案 0 :(得分:0)

我假设您希望它进入if (ar.length > 1)区块 - 如果是这样的话,就像这样:

ar = $(ar).first();

会给你第一个元素。您还可以使用.eq()获取零索引元素:

ar = $(ar).eq(0); // returns the first element