【问题标题】:JQuery/Javascript Append HTML Table Data To TBODY, Double OutputJQuery/Javascript 将 HTML 表数据附加到 TBODY,双输出
【发布时间】:2015-02-26 21:22:03
【问题描述】:

大家早上好。我有一个问题会阻止我的项目进展,直到我能够完成它。所以,我想请各位好心人帮忙!

情况是这样的:我必须为用户提供一个动态增长的表,允许他们输入 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中的相关代码,以及下面(用于提问目的的sn-ps)...

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);
});

有人可以帮助我了解每次单击按钮时我如何/为什么会获得重复的行条目吗?

提前致谢!!

【问题讨论】:

标签: javascript jquery html


【解决方案1】:

您的 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 现代浏览器确保 tr 元素在 dom 中被 tbody 包裹。这样,您最终会得到 2 个与要应用附加的元素的选择器匹配的元素。

【讨论】:

  • 准确!一旦我按照建议添加了&lt;thead&gt;,我的 dup 问题就消失了。感谢您的快速周转!
【解决方案2】:

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

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

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

【讨论】:

  • 这导致新行完全替换现有行,这不是我想要做的......谢谢你的提议!
猜你喜欢
  • 2018-04-06
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多