使用jquery填充多选框

时间:2011-08-31 04:44:14

标签: jquery multi-select populate

我可以使用jquery填充dropdownlist,如下所示:

下拉列表:

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

脚本代码:

$(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");
            }
        });

});

带有省列表功能的文件function.aspx:

[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;
    }

如何通过上述方式填充多选框,请帮助我。非常感谢。 (我使用多选框插件http://www.erichynds.com/jquery/jquery-ui-multiselect-widget/,但我可以填充来自服务器的数据)

1 个答案:

答案 0 :(得分:11)

不是很清楚,但我认为在您完成向select附加选项后,您需要刷新它

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

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

也可以代替.removeAttr启用和禁用多选

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

P.S:您正在按ID选择下拉列表,它类似于$("#province") 不喜欢 $("province")

查看jquery id selectors

您的完整代码可能看起来像

<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");
            }
        });

});