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