1. 程式人生 > >Jquery——動態載入 option

Jquery——動態載入 option

老是忘,記錄一下

html:

 <div class="col-md-4">                    
     <label class="control-label col-md-4">省份</label>                       
     <select class="form-control selectpicker" id="ProvinceId"name="ProvinceId" >
     </select>
                       
 </div>

js:

    function GetProvince() {
            $("select[name=ProvinceId]").empty();
            $.ajax({
                type: "GET",
                dataType: "json",
                url: "@Url.Action("getProvinceList")",
                success: function (res) {
                    debugger
                    var option = "<option value=\"\">請選擇</option>";
                    for (var i = 0; i < res.length; i++) {
                        option+="<option value=\"" + res[i].Value + "\">" + res[i].Text + "</option>";
                    }
                    $("select[name=ProvinceId]").append(option);
                    $("select[name=ProvinceId]").selectpicker('refresh');
                }
            })

        }

如果option列表沒有值,重新整理一下selectpicker;

 $("select[name=ProvinceId]").selectpicker('refresh');