使用jquery将项动态绑定到下拉列表

时间:2013-11-05 20:21:56

标签: jquery

如何将dropdowncheckbox列表动态绑定到页面中的值? 我使用的是jquery多选插件: http://wenzhixin.net.cn/p/multiple-select/#acknowledgements

我的jquery版本是1.7.2

我不能让jsfiddle使用插件。 我有一个奇怪的问题,脚本src stmt不能位于布局页面(MVC)的头部。所以我把它放在布局页面的底部,如下所示:

</body>
<script src="~/Scripts/jquery.multiple.select.js"></script>
</html>

在视图中: 选择列表:

<select id="labTestFilter" name="labTestFilter" multiple="multiple" style="width: 200px;">
    @*the values of the select list will be populated by the javascript function*@
</select>

但是对于使用jquery的任何列表类型,它应该仍然相同。

这是jsfiddle: http://jsfiddle.net/vnVTx/4/

<script type="text/javascript">
    $(function bindLabTestFilter() {
        // get the list of unique test names on the page and bind it to the select list control
        var a = $(".labtestname").map(function () { return this.innerHTML; }).get();
        a = jQuery.unique(a);
});
</script>

1 个答案:

答案 0 :(得分:0)

解决了我的问题:

选择列表:

<select id="labTestFilter" name="labTestFilter" multiple="multiple" style="width: 200px;">
    @*the values of the select list will be populated by the javascript function*@
</select>

Jquery的:

<script type="text/javascript">
    $(function bindLabTestFilter() {
        // get the list of unique test names on the page and bind it to the select list control
        var a = $(".labtestname").map(function () { return this.innerHTML; }).get();
        a = jQuery.unique(a);
        for (var i = 0; i < a.length; i++) {
            $("#labTestFilter").append("<option value=\"" + a[i] + "\">" + a[i] + "</option>");
        }
        //converts the select list into a drop down checklist using jquery.multiple.select plugin
        $('#labTestFilter').multipleSelect();
    });

    $(".ms-drop").click(function () {
        $(function showSelectedValues() {
            alert($(".ms-drop input[type=checkbox]:checked").map(
               function () { return this.value; }).get().join(","));
        });
    });

</script>