/**
* 全局select控制
* 实现逻辑
* 1:显示逻辑
* change后,字符过长, append个只为显示得option
* 2:动态计算长度逻辑
* visibleWidth: 取selec框显示区宽度(clientWidth)-54(PD+背景图宽度)-4(省略号位置)
* lenghts: 除6(英文是6汉字是12)为显示区全英文长度
* 算取长度范围内汉字个数(长度减去汉字个数)
* 注: font-size: 14px 文字大小基准
*/
function onSelectChange () {
document.addEventListener('change',
function (type,listener,useCapture) {
$(".optionselected").remove();//作为显示得option
if(type.target.id){
console.log($("#"+type.target.id));
var options=$("#"+type.target.id).find("option:selected").text();
var visibleWidth=$("#"+type.target.id)[0].clientWidth;
var lenghts=Math.floor((visibleWidth-54)/6);
if(options.length>lenghts){
options = options.substring(0,lenghts);
var len = 0;
for (var i = 0; i < options.length; i++) {
var a = options.charAt(i);
if (a.match(/[^\x00-\xff]/ig) != null) {
len += 1;
}
}
$("#"+type.target.id).append('<option class="optionselected" selected="selected" style="display: none" value=\''+$("#"+type.target.id).val()+'\'>'+
options.substring(0,Number(lenghts-
Math.floor(len/2)
))+ '...'+'</option>')
}else {
return;
}
}
},
false);
}