搜尋此網誌

2012-02-23

jQuery optgroup

        <select id="DropDownList1">
            <option value="1" optgroup="9910">擔任專業考試命題委員</option>
            <option value="2" optgroup="9910">擔任學會行政職務</option>
            <option value="3" optgroup="9903">擔任專業考試命題委員</option>
            <option value="4" optgroup="9903">擔任學會行政職務</option>
            <option value="5" optgroup="98">擔任學會行政職務</option>
            <option value="6" optgroup="97">擔任學會行政職務</option>
        </select>
        $(document).ready(function () {
            
            var a = $("select#DropDownList1 option[optgroup]");
            var thisAttr = "";
            var prevAttr = "";
            a.each(function (index) {
                prevAttr = thisAttr;
                thisAttr = $(this).attr("optgroup");
                if (thisAttr != prevAttr) {
                    $("select#DropDownList1 option[optgroup=" + thisAttr + "]").wrapAll("<optgroup label='" + thisAttr + "'>");
                }
            });
            
        });
NOTE:此做法在FIREFOX 11 POSTBACK時,會有selected選項顯示錯誤的問題,IE 9, CHROME 18正常,待修正.

沒有留言: