【问题标题】:jQuery: How to append checkbox to each row in table?jQuery:如何将复选框附加到表中的每一行?
【发布时间】:2012-06-05 05:33:17
【问题描述】:

我有这个用于从 json 中追加元素:

$.ajax({
                type: "POST",
                url: action,
                data: dataSearch,
                success: function (response) {

                    if (response[0].success == "success") {
                        $.each(response, function (index, record) {
                            var row = $("<tr />");
                            $("<td />").text(record.clavemat).appendTo(row);
                            $("<td />").text(record.tipomat).appendTo(row);
                            $("<td />").text(record.titulomat).appendTo(row);
                            $("<td />").text(record.autormat).appendTo(row);
                            $("<td />").text(record.editmat).appendTo(row);
                            $("<td />").text(record.edicmat).appendTo(row);
                            //Append checkbox to each row
                            row.addClass("alt");
                            row.appendTo(".resultsSearch");
                        });
                    } else {
                        alert("Data not found");
                    }

                }
            });
            return false;

我想在最后一列的每一行附加复选框,我不知道 谢谢!

【问题讨论】:

    标签: jquery append html-table


    【解决方案1】:

    你试过了吗?:

    $("<td />").html('<input type="checkbox"/>').appendTo(row);
    

    顺便说一句,如果您从服务器获取信息,您确定 POST 是最佳选择吗?或者您在执行请求时是否更改了服务器的状态? 如果不是,请查看When to use Post or Get

    【讨论】:

    • 之后我添加了复选框......为什么忽略这个功能? $('table tr').click(function() { check = $(this).children('td').children('input[type=checkbox]'); if (check.attr('checked') ) check.attr('checked',false); 否则 check.attr('checked',true); });用于检查单击该行的任何部分时选中的复选框。
    • 如果你在document.ready的时候添加这个函数,它不会起作用,因为行还没有创建,你应该使用.on
    • 它有效:,再次感谢!顺便说一句,我希望你不介意你添加到 facebook(Edgar Ramirez,SoldierCorp)...问候!
    猜你喜欢
    • 2020-06-05
    • 2019-10-20
    • 2017-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-23
    • 1970-01-01
    相关资源
    最近更新 更多