缩短选择选项列表中的长选项

时间:2013-04-03 20:51:53

标签: jquery list options

我希望能够将选项菜单中的任何项目缩短,具体取决于它的字符大小,我的尝试如下:

<script>
    jQuery(document).ready(function() {

        jQuery("#groupId").each(function(){     
        var myoptions = jQuery('option').length;
        var shorter =  myoptions.substring(0,10)+"...";     

        if (('myoptions')>10){  
            jQuery(this).find('option').text(shorter)

        }


                }); 

         });
    </script>

HTML

<select id="groupId"  name="groupId">
<option value="0">Select...</option>
<option selected="selected" value="1074">Nomee Project Owners Group</option>
<option value="1064">Non-Admin Group this is too long this is too long and I want it to be shortened to 10 chars with ...</option>
<option value="1043">norights</option>
<option value="1018">Promotions Group</option>
<option value="1082">PS Repeater Group</option>
<option value="1013">QC Group</option>
<option value="1056">Ryan-Group A</option>
<option value="1000">USA - SDS Admin</option>
</select>

1 个答案:

答案 0 :(得分:2)

你可以尝试这个http://jsfiddle.net/BJ8WK/

$("#groupId option").each(function()
{
    if($(this).text().length>10)
     {
        $(this).text($(this).text().substring(0, 10));
     }
});