gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 11:54:10 26 4
gpt4 key购买 nike

大家早上好。我遇到一个问题,该问题阻碍了我的项目进展,直到我能够解决它。所以,我想向各位好心人寻求帮助!

情况是这样的:我必须向用户提供一个动态增长的表,允许他们输入 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);
});

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

提前致谢!!

最佳答案

您的 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 个与要应用附加的元素的选择器相匹配的元素。

关于javascript - JQuery/Javascript 将 HTML 表数据附加到 TBODY,双输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28743425/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com