最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JQueryJavascript Append HTML Table Data To TBODY, Double Output - Stack Overflow

programmeradmin6浏览0评论

Good morning all. I have an issue that's stopping my project progression until I can get through it. So, I thought I'd ask you fine folks for an assist!

The situation is this: I have to provide a dynamically growing table to the users that will allow them to enter 1-N records for returns. So they load the page, click the button, and a new row appears. I am glad to say that my code works, with one major flaw - the table rows repeat!! So if the user were to click the "Add" button twice, I'd expect to have them see:

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

Instead they see this:

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

It's the weirdest thing to me. This code's not in a loop or anything - so it should just get one row!! Relevant code in my Fiddle here, as well as below (snippets for question purposes)...

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

Can someone out there help me understand how/why I'm getting a duplicate row entry for each time the button is clicked?

Thanks in advance!!

Good morning all. I have an issue that's stopping my project progression until I can get through it. So, I thought I'd ask you fine folks for an assist!

The situation is this: I have to provide a dynamically growing table to the users that will allow them to enter 1-N records for returns. So they load the page, click the button, and a new row appears. I am glad to say that my code works, with one major flaw - the table rows repeat!! So if the user were to click the "Add" button twice, I'd expect to have them see:

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

Instead they see this:

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

It's the weirdest thing to me. This code's not in a loop or anything - so it should just get one row!! Relevant code in my Fiddle here, as well as below (snippets for question purposes)...

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

Can someone out there help me understand how/why I'm getting a duplicate row entry for each time the button is clicked?

Thanks in advance!!

Share Improve this question asked Feb 26, 2015 at 13:12 TheJester1977TheJester1977 1551 silver badge10 bronze badges 3
  • Change $('#retc > tbody').append(chtml) to $('#retc > tbody').html(chtml) – lshettyl Commented Feb 26, 2015 at 13:15
  • possible duplicate of Adding rows to tbody of a table using jquery – user557419 Commented Feb 26, 2015 at 13:18
  • @Daniel-Ziga - Nope, not a duplicate... :) – TheJester1977 Commented Feb 26, 2015 at 13:43
Add a ment  | 

2 Answers 2

Reset to default 6

Your html fragment is lacking a thead element. The following structure of the table element makes it work.

<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>

Live Demo here.

Explanation

iirc modern browsers make sure that tr elements are wrapped with a tbody in the dom. This way you end up with 2 elements matching the selector of the element(s) on which to apply the appending.

You need to either empty the cotainer first or use .html

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

OR

$('#retc > tbody').html(chtml);
发布评论

评论列表(0)

  1. 暂无评论