将行附加到表中

时间:2016-07-19 18:58:21

标签: javascript jquery html

我试图将一个充满对象的数组附加到表的主体中。

var tourTable = [];
var event = {
    date: eventDate,
    hour: eventOra
}
bookTable.push(event);

for(var i = 0; i < bookTable.length; i++) {
    $('#tour-events tbody').append('');
}

// table
<table id="tour-events">
    <thead></thead>
    <tbody></tbody>
</table>

我第一次单击一个按钮时一切似乎都很好,但是第二次尝试它时会添加比应该更多的元素。我在控制台中检查了数组并且元素数量很好。你有什么建议和其他方法吗?

2 个答案:

答案 0 :(得分:0)

您在variable声明array名称时错误bookTable名称应该是tourTable而不是var bookTable = []; var event = { date: eventDate, hour: eventOra } bookTable.push(event); for(var i = 0; i < bookTable.length; i++) { $('#tour-events tbody').append(''); } // table <table id="tour-events"> <thead></thead> <tbody></tbody> </table>

from __future__ import unicode_literals
from django.contrib.auth.models import User
from django_countries.fields import CountryField
from django.db import models

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    name = models.CharField(max_length=100)
    bio = models.TextField(max_length=1000, blank=True, null=True)
    image = models.FileField()
    country = CountryField()
    city = models.CharField(max_length=100)
    twitter = models.CharField(max_length=100, null=True, blank=True)
    linkedin = models.CharField(max_length=100, null=True, blank=True)
    location = models.TextField()

    def __unicode__(self):
        return self.name

    def __str__(self):
        return self.name


class Mentor(models.Model):
    mentor = models.CharField(max_length=100)
    mentee = models.CharField(max_length=100)

    def __unicode__(self):
        return self.mentee

    def __str__(self):
        return self.mentee

答案 1 :(得分:0)

在顶部添加以下行:

$('#tour-events tbody').empty();
var bookTable = [];