gpt4 book ai didi

javascript - 使用 JS/jQuery 将行动态添加到表时行消失

转载 作者:行者123 更新时间:2023-11-29 18:11:04 25 4
gpt4 key购买 nike

我正在尝试创建一个可以随意添加行的表格。有很多问题可以解决这个问题,但我找不到可以解决我遇到的问题的问题。这些行在创建后立即消失,并且在检查时不会反射(reflect)在 html 中 (f12)。我看过 this question还有this popular one他们没有解决这个问题。这是我的 html:

<form id="taskList" style="width:60%">
<fieldset>
<table id="taskTable" style="width:100%">
<tr>
<th style="text-align:left">Task Number</th>
<th style="text-align:left"><abbr title="Total amount of time the task needs to run to finish execution.">Computation Time</abbr></th>
<th style="text-align:left"><abbr title="Does the task need exclusive use of the resource?">Resource Needed</abbr></th>
<th style="text-align:left"><abbr title="How often the task will repeat itself. Acts as a deadline since it must complete before restarting.">Deadline/Period</abbr></th>
<th style="text-align:left"><abbr title="Any number representing a task's priority. Higher numbers will be given precedence.">Priority</abbr></th>
</tr>
<tr>
<td>T1</td>
<td><input id="compTime_T1" value="5"/></td>
<td>
<select id="resNeeded_T1" name="res_T1">
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
</td>
<td><input id="period_T1" value="20"/></td>
<td><input id="priority_T1" value="0"/></td>
</tr>
</table>
<button id="addTaskButton">Add task...</button><br><br>
<input type="button" id="submitTaskSet" value="Generate Schedule"/>
<input type="button" id="resetTable" value="Clear Schedule"/>
</fieldset>
</form>

这是我原来的 javascript(在尝试上述问题中建议的方法之前,效果更差)。

$('#addTaskButton').click(function () {addTaskToTable();});
$('#submitTaskSet').click(function () {generateSchedule();});
$('#resetTable').click(function () {resetTaskSet();});

//Variables
var taskTableRows = 1;

function addTaskToTable() {
taskTableRows += 1;
var taskNumStr = taskTableRows.toString();
var table = document.getElementById("taskTable");
var row = table.insertRow(taskTableRows);
var cellTaskNum = row.insertCell(0);
var cellCompTime = row.insertCell(1);
var cellRes = row.insertCell(2);
var cellPeriod = row.insertCell(3);
var cellPrio = row.insertCell(4);
cellTaskNum.innerHTML = "T" + taskNumStr;
cellCompTime.innerHTML = "<input id=\"compTime_T" + taskNumStr + "\" value=\"5\"/>";
cellRes.innerHTML = "<select id=\"resNeeded_T" + taskNumStr + "\" name=\"res_T" +
taskNumStr + "\">" + "<option value=\"yes\">Yes</option>" +
"<option value=\"no\">No</option></select>";
cellPeriod.innerHTML = "<input id=\"period_T" + taskNumStr + "\" value=\"20\"/>";
cellPrio.innerHTML = "<input id=\"priority_T" + taskNumStr + "\" value=\"0\"/>";
}

这是一个jsFiddle . (编辑:使用不同的方法以免破坏 JSFiddle)

我不知道这是否可能是特定于浏览器的问题,因为有时它似乎在 IE11 中有效,但在 Firefox 或 Chrome 中却从来没有。任何建议将不胜感激。

最佳答案

你有一个表单中的按钮,它的默认操作是提交表单,所以通过调用 event.preventDefault() 来阻止它。在点击处理程序中

$('#addTaskButton').click(function(e) {
e.preventDefault()
taskTableRows += 1;
var taskNumStr = taskTableRows.toString();
//var table = document.getElementById("taskTable");
//var row = table.insertRow(taskTableRows);
//var cellTaskNum = row.insertCell(0);
//var cellCompTime = row.insertCell(1);
//var cellRes = row.insertCell(2);
//var cellPeriod = row.insertCell(3);
//var cellPrio = row.insertCell(4);
var newRow = "<tr><td>T" + taskNumStr + "</td>" +
"<td><input id=\"compTime_T" + taskNumStr + "\" value=\"5\"/></td>" +
"<td><select id=\"resNeeded_T" + taskNumStr + "\" name=\"res_T" + taskNumStr + "\">" +
"<option value=\"yes\">Yes</option>" +
"<option value=\"no\">No</option></select></td>" +
"<td><input id=\"period_T" + taskNumStr + "\" value=\"20\"/></td>" +
"<td><input id=\"priority_T" + taskNumStr + "\" value=\"0\"/></td></tr>";
$('#taskTable tr:last').after(newRow);
});

演示:Fiddle

关于javascript - 使用 JS/jQuery 将行动态添加到表时行消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27392899/

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