Shorten long options in a select options list

走远了吗. 提交于 2019-12-08 06:22:04

问题


I would like to be able to make any items in an options menu shorter depending on it's character size my attempt is as follows:

<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:


you can try this http://jsfiddle.net/BJ8WK/

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


来源:https://stackoverflow.com/questions/15797865/shorten-long-options-in-a-select-options-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!