﻿//获取select项的个数
(function($) {
    $.fn.sizes = function() {
        return $(this).get(0).options.length;
    }
    $.fn.getSelectedIndex = function() {
        return $(this).get(0).selectedIndex;
    }
    $.fn.getSelectedText = function() {
        if (this.size() == 0) {
            return "下拉框中无选项";
        } else {
            var index = this.getSelectedIndex();
            return $(this).get(0).options[index].text;
        }
    }
    //获取当前选中项的值
    $.fn.getSelectedValue = function() {
        if (this.size() == 0) {
            return "下拉框中无选中值";
        } else {
            return $(this).val();
        }
    }
    $.fn.setSelectedValue = function(value) {
        $(this).get(0).value = value;
    }
    $.fn.setSelectedText = function(text) {
        var isExists = false;
        var count = this.sizes();
        for (var i = 0; i < count; i++) {
            if ($(this).get(0).options[i].text == text) {
                $(this).get(0).options[i].selected = true;
                isExists = true;
                break;
            }
        }
        if (!isExists) {
            alert("下拉框中不存在该项");
        }
    }
    $.fn.setSelectedIndex = function(index) {
        var count = this.sizes();
        if ((index >= count) || (index < 0)) {
            alert("选中项索引超出最大范围");
        } else {
            $(this).get(0).selectedIndex = index;
        }
    }
    $.fn.isExistItem = function(value) {
        var isExists = false;
        var count = this.sizes();
        for (var i = 0; i < count; i++) {
            if ($(this).get(0).options[i].value == value) {
                isExists = true;
                break;
            }
        }
        return isExists;
    }
    $.fn.addOption = function(text, value) {
        if (this.isExistItem(value)) {
            alert("待添加项的值已存在");
        } else {
            $(this).get(0).options.add(new Option(text, value));
        }
    }
    $.fn.removeItem = function(value) {
        if (this.isExistItem(value)) {
            var count = this.sizes();
            for (var i = 0; i < count; i++) {
                if ($(this).get(0).options[i].value == value) {
                    $(this).get(0).remove();
                    break;
                }
            }
        } else {
            alert("待删除的项已不存在");
        }
    }
    $.fn.removeIndex = function(index) {
        var count = this.sizes();
        if (index >= count || index < 0) {
            alert("待删除项索引超出范围");
        }
        else {
            $(this).get(0).remove(index);
        }
    }
    $.fn.removeSelected = function() {
        var index = this.getSelectedIndex();
        this.removeIndex(index);
    }
    $.fn.clearAllItems = function() {
        $(this).get(0).options.length = 0;
    }
    $.fn.clearSelected = function() {
        var count = this.sizes();
        $(this).get(0).selectedIndex = -1;
    }
    $.fn.setmulitSelected = function(value) {
        var count = this.sizes();
        for (var i = 0; i < count; i++) {
            if ($(this).get(0).options[i].value == value) {
                $(this).get(0).options[i].selected = true;
            }
        }
    }
    $.fn.getmultiSelectedValues = function() {
        var s = '';
        var count = this.sizes();
        for (var i = 0; i < count; i++) {
            if ($(this).get(0).options[i].selected == true) {
                s += ',' + $(this).get(0).options[i].value;
            }
        }
        if (s != '') { s = s.substr(1); }
        return s;
    }
})(jQuery);
