【问题标题】:Populating checkboxes填充复选框
【发布时间】:2014-02-09 16:53:01
【问题描述】:

如何使用 jQuery 在复选框中添加项目?
要挂载下拉列表,请使用以下命令:

$.each(dataResult, function (index, itemData) {
    select.append($('<option/>', {
        value: itemData.Value,
        text: itemData.Text
    }));
});

我需要知道构建一个复选框?

【问题讨论】:

    标签: jquery asp.net-mvc checkbox


    【解决方案1】:

    请尝试如下。

    <script type="text/javascript" language="javascript">
            function PopulateCheckBoxList() {
                $.ajax({
                    type: "POST",
                    url: "CheckBoxList/GetCheckBoxDetails",
                    contentType: "application/json; charset=utf-8",
                    data: "{}",
                    dataType: "json",
                    success: AjaxSucceeded,
                    error: AjaxFailed
                });
            }
            function AjaxSucceeded(result) {
                BindCheckBoxList(result);
            }
            function AjaxFailed(result) {
                alert('Failed to load checkbox list');
            }
            function BindCheckBoxList(result) {
    
                var items = JSON.parse(result.d);
                CreateCheckBoxList(items);
            }
            function CreateCheckBoxList(checkboxlistItems) {
                var table = $('<table></table>');
                var counter = 0;
                $(checkboxlistItems).each(function () {
                    table.append($('<tr></tr>').append($('<td></td>').append($('<input>').attr({
                        type: 'checkbox', name: 'chklistitem', value: this.Value, id: 'chklistitem' + counter
                    })).append(
                    $('<label>').attr({
                        for: 'chklistitem' + counter++
                    }).text(this.Name))));
                });
    
                $('#dvCheckBoxListControl').append(table);
            }
        </script>
    

    请查看下面提到的链接以获取更多信息。

    注意:这是针对网络表单的。但您也可以轻松地将它与 MVC 一起使用。

    Bind a CheckBox list from database using jQuery AJAX

    【讨论】:

    • 非常感谢您的帮助,这正是我要找的,只需放入 paramnetros 函数,它就可以完美运行。
    • @rysahara 很高兴听到它有帮助!谢谢 :)
    猜你喜欢
    • 2019-04-18
    • 1970-01-01
    • 2014-11-08
    • 1970-01-01
    • 1970-01-01
    • 2022-01-02
    • 2016-10-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多