Populate multiselect box using jquery

为君一笑 提交于 2019-12-08 21:04:34

问题


i can populate dropdownlist using jquery as below :

Dropdownlist :

<select id="province"></select>

Script code :

$(document).ready(function(){
    $.ajax({
            type: "POST",
            url: "function.aspx/provincelist",
            data: "",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function OnPopulateControl(response) {
                list = response.d;
                if (list.length > 0) {
                    $("province").removeAttr("disabled");
                    $("province").empty().append('<option value="0">Please select</option>');
                    $.each(list, function () {
                        $("province").append($("<option></option>").val(this['Value']).html(this['Text']));
                    });
                    $("province").val(valueselected);
                }
                else {
                    $("province").empty().append('<option selected="selected" value="0">Not available<option>');
                }
            },
            error: function () {
                alert("Error");
            }
        });

});

File function.aspx with provincelist function :

[System.Web.Services.WebMethod]
    public static ArrayList provincelist()
    {
        ArrayList List = new ArrayList();
        SqlConnection con = DBclass.moketnoi();
        SqlCommand cmd = new SqlCommand("SELECT TC_CODE, TC_NAME FROM PM_PROVINCE", con);
        SqlDataReader sdr = cmd.ExecuteReader();
        while (sdr.Read())
        {
            List.Add(new ListItem(
                sdr["TC_NAME"].ToString(),
                sdr["TC_CODE"].ToString()
                ));
        }
        con.Close();
        return List;
    }

How can I populate multi-select box by the same way above, please help me. Thanks so much. (i use multi-select box plugin http://www.erichynds.com/jquery/jquery-ui-multiselect-widget/, but I can populate with data from server )


回答1:


not very clear but i think after you are done appending the options to the select you need to refresh it like

$("#province").multiselect('refresh');

see here http://www.erichynds.com/examples/jquery-ui-multiselect-widget/demos/#refresh

also instead of .removeAttr you can enable and disable the multi-select

http://www.erichynds.com/examples/jquery-ui-multiselect-widget/demos/#enabledisable

P.S: you are selecting the dropdown by id and it goes like $("#province") NOT like $("province")

have a look at jquery id selectors

your complete code may look like

<select id="province" multiple="multiple"></select>

-

$(document).ready(function(){
    var $select = $("#province").multiselect();//apply the plugin
    $select.multiselect('disable'); //disable it initially
    $.ajax({
            type: "POST",
            url: "function.aspx/provincelist",
            data: "",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function OnPopulateControl(response) {
                list = response.d;
                if (list.length > 0) {
                    $select.multiselect('enable');
                    $("#province").empty().append('<option value="0">Please select</option>');
                    $.each(list, function () {
                        $("#province").append($("<option></option>").val(this['Value']).html(this['Text']));
                    });
                    $("#province").val(valueselected);
                }
                else {
                    $("#province").empty().append('<option selected="selected" value="0">Not available<option>');
                }
              $("#province").multiselect('refresh'); //refresh the select here
            },
            error: function () {
                alert("Error");
            }
        });

});


来源:https://stackoverflow.com/questions/7252633/populate-multiselect-box-using-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!