简体   繁体   English

为什么我的jquery代码没有在运行时向html表添加行?

[英]Why doesn't my jquery code add rows to html table on fly?

I have written this code to add rows and columns on the fly to my html table but it doesn't. 我已经编写了这段代码,以将行和列动态添加到我的html表中,但事实并非如此。 Why? 为什么?

$.each(response.lstTimeSlotsReturned, function (i, slots)
                {                        
                    $("#tbodytblAvailableAppointments").find('tbody').append($('<tr>').append($('<td>').html('Td')));
                }); 

Full code: 完整代码:

$('form').submit(function (e) {
    e.preventDefault();
    if (!$(this).valid()) {
        return;
    }

    var url = '@Url.Action("ShowAvailableAppointments")';
    var data = $(this).serialize();

    $.post(url, data, function (response) {

        if (response.ReturnStatusJSON == true) {
            swal("Booked !", "Done", "success");

            $.each(response.lstTimeSlotsReturned, function (i, slots)
            {                        
                $("#tbodytblAvailableAppointments").find('tbody').append($('<tr>').append($('<td>').html('Td')));
            });                    
        }
        else {
            swal("Sorry !", "Failed", "error");                  
        }
    });
});

Update: HTML table 更新:HTML表

 <div class="container">
        <div class="table-responsive">
            <table id="tblAvailableAppointments" class="table table-condensed">

                <thead>
                    <tr>
                        <th>S.No</th>
                        <th>Timings</th>
                        <th>Date</th>
                    </tr>
                </thead>
                <tbody id="tbodytblAvailableAppointments">

                </tbody>

            </table>
        </div>
    </div>

I have tried many other strategies but none worked. 我尝试了许多其他策略,但没有一个奏效。 It has taken a lot of time. 花了很多时间。

Your JS is trying to select tbody under element with id #tbodytblAvailableAppointments . 你的JS试图选择tbody元素下id为#tbodytblAvailableAppointments But that is id of the tbody itself. 但这是tbody本身的ID。 Just remove the find('tbody') part: 只需删除find('tbody')部分:

$("#tbodytblAvailableAppointments").append($('<tr>').append($('<td>').html('Td')));

$("#tbodytblAvailableAppointments").find('tbody')

#tbodytblAvailableAppointments已经是tbody ,因此您找不到所需的tbody。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM