JQuery / Javascript将HTML表数据附加到TBODY,双输出

时间:2015-02-26 13:12:56

标签: javascript jquery html

大家早上好。我有一个问题是阻止我的项目进展,直到我能够完成它。所以,我以为我会请你们好帮忙!

情况是这样的:我必须向用户提供一个动态增长的表,允许他们输入1-N个记录以便返回。因此,他们加载页面,单击按钮,然后会出现一个新行。我很高兴地说我的代码有效,有一个主要缺陷 - 表格行重复!!因此,如果用户要点击"添加"按钮两次,我希望让他们看到:

+---+------+------+----------+
| # | Year | week | Quantity |
+---+------+------+----------+
| 1 |   |v||   |v|| ________ |
+---+------+------+----------+
| 2 |   |v||   |v|| ________ |
+---+------+------+----------+

相反,他们看到了这一点:

+---+------+------+----------+
| # | Year | week | Quantity |
+---+------+------+----------+
| 1 |   |v||   |v|| ________ |
+---+------+------+----------+
| 2 |   |v||   |v|| ________ |
+---+------+------+----------+
| 1 |   |v||   |v|| ________ |
+---+------+------+----------+
| 2 |   |v||   |v|| ________ |
+---+------+------+----------+

这对我来说是最奇怪的事情。这段代码不在循环或任何东西 - 所以它应该只有一行!我的Fiddle here以及下方的相关代码(用于问题目的的代码段)......

HTML

<fieldset class="fieldset2">
    <legend>Return Specific Curriculum Information</legend>
    <input type="hidden" id="ccnt" value="0">
    <table class="table" id="retc">
       <tr>
           <th class="th">Entry #</th>
           <th class="th">Year</th>
           <th class="th">Week/Packet</th>
           <th class="th">Quantity</th>
       </tr>
       <tbody>

       </tbody>
    </table>
    <input type="button" value="Add Curriculum To Return" class="button" id="addcurric">
    <input type="button" value="Remove All Entries" class="button" id="remcurric">
</fieldset>

的JQuery / JavaScript的

$('#ccnt').data('count', 0);
$('#addcurric').click(function () {
    function getcount() {
        var $this = $('#ccnt'),
        count = $this.data('count') + 1;

        $this.data('count', count);
        return count;
    }

    var mycount = getcount();
    //console.log('mycount: ' + mycount);

    var tdclass;
    if (mycount % 2 == 1) {
        tdclass = "td1";
    } else {
        tdclass = "td2";
    }
    //console.log('tdclass: ' + tdclass);
    //window.alert(mycount + ' ' + tdclass);

    var chtml = "";
    chtml += "'<tr>";
    chtml += "    <td class=\"" + tdclass + "\">" + mycount + "</td>\\n";
    chtml += "    <td class=\"" + tdclass + "\"><select name=\"HYear" + mycount + "\" id=\"hyear" + mycount + "\">\\n";
    chtml += "        <option value=\"0\">-- Select --</option>\\n";
    chtml += "        <option value=\"1\">Year 1</option>\\n";
    chtml += "        <option value=\"2\">Year 2</option>\\n";
    chtml += "        <option value=\"3\">Year 3</option>\\n";
    chtml += "    </select></td>\\n";
    chtml += "    <td class=\"" + tdclass + "\"><select name=\"Week" + mycount + "\" id=\"week" + mycount + "\">\\n";
    chtml += "        <option value=\"0\">-- Select --</option>\\n";
    chtml += "        <option value=\"1\">Week 1</option>\\n";
    chtml += "        <option value=\"2\">Week 2</option>\\n";
    chtml += "        <option value=\"3\">Week 3</option>\\n";
    chtml += "    </select></td>\\n";
    chtml += "    <td class=\"" + tdclass + "\"><input type=\"text\"  name=\"qty" + mycount + "\" class=\"input\"></td>\\n";
    chtml += " </tr>\\n'";

    //console.log('chtml is: ' + chtml);
    //window.alert(chtml);
    //console.log("Writing an iteration of chtml to scrren...");
    $('#retc > tbody').append(chtml);
});

有人可以帮助我了解每次点击按钮时我是如何/为什么会收到重复的行条目?

提前致谢!!

2 个答案:

答案 0 :(得分:6)

您的html片段缺少thead元素。表元素的以下结构使其有效。

<table class="table" id="retc">
    <thead>
        <tr>
            <th class="th">Entry #</th>
            <th class="th">HIPPY Year</th>
            <th class="th">Week/Packet</th>
            <th class="th">Quantity</th>
        </tr>
    </thead>
   <tbody>

   </tbody>
</table>

现场演示here

<强>解释

iirc现代浏览器确保{d}中的tr元素包含tbody。这样,您最终会得到2个元素,这些元素与要应用追加的元素的选择器相匹配。

答案 1 :(得分:1)

您需要先清空容器或使用.html

$('#retc > tbody').empty().append(chtml);

OR

$('#retc > tbody').html(chtml);