【问题标题】:Shorten long options in a select options list缩短选择选项列表中的长选项
【发布时间】:2013-04-03 20:51:53
【问题描述】:

我希望能够根据字符大小缩短选项菜单中的任何项目我的尝试如下:

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

【问题讨论】:

    标签: jquery list options


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-13
      • 1970-01-01
      • 2014-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多