这就是我想知道的。我有一个表,它的行数取决于旋转器中的数字。这是可行的,例如,如果我在spinner中输入25,它会显示25行,如果我在spinner中输入7,它会显示7行。
所以我的问题是:
假设一个表中有许多行。我有一个文本区,用户输入他们的问题,然后提交问题,问题应该插入并出现在表格的第一行,在"问题"列下,文本区变为空白,用户输入他的第二个问题,如果用户提交这个问题,那么这个问题将出现在第二行,第三个问题出现在第三行,第四个问题出现在第四行,等等
问题是我不知道该怎么做。有人能告诉我如何做到这一点吗?我不是一个强大的Javascript程序员,我更一个Oracle和MYSQL程序员,但我需要使用Javascript为我的项目。
任何帮助都将不胜感激
下面是我的Javascript代码: <script type="text/javascript">
function insertQuestion() {
var qandatable = document.getElementById("qandatbl");
var questionDiv = document.getElementById("question");
var getQuestion = document.getElementById("questionTextarea");
var rowCount = qandatable.rows.length;
var row = qandatable.insertRow(rowCount);
var questionCell = row.insertCell(getQuestion);
questionCell.innerHTML = getQuestion.value;
}
</script>
下面是html代码:
//table where questions would be stored
<table id="qandatbl" align="center">
<thead>
<tr>
<th><span id="qidhead">Question No</span></th>
<th><span id="questionhead">Question</span></th>
</tr>
</thead>
<tbody>
<?php
$spinnerCount = $_POST['textQuestion'];
if($spinnerCount > 0) {
for($i = 1; $i <= $spinnerCount; $i++) {?>
<tr>
<td id="qid"><?php echo $i; ?></td>
<td id="question"></td>
</tr>
</tbody>
<?php
}
}
?>
</table>
//table which consists of textarea and submit button
<form id="enter" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<table id='middleDetails' border='1'>
<tr>
<th class='tblheading' colspan='2'>SESSION DETAILS</th>
</tr>
<tr>
<td id="questionNum">Question No </td>
</tr>
<tr>
<td id="questionContent">Question:</td>
<td id="questionTextarea"><textarea rows="5" cols="40" id="questionTxt" name="questionText"></textarea></td>
</tr>
<tr>
<td id="addQuestionRow" colspan='2'><input id="addQuestion" type="button" value="Add Question" name="addQuestionBtn" onClick="insertQuestion()" /></td>
</tr>
</table>
</form>
首先必须创建行和单元格元素,然后才能将它们添加到表中。
var table = document.getElementById("qandatbl");
var tableBody = table.tBodies[0];
var textarea = document.getElementById("questionTxt");
var row = document.createElement("tr");
tableBody.appendChild(row);
var enumeratorCell = document.createElement("td");
enumeratorCell.className = "qid";
row.appendChild(enumeratorCell);
var questionCell = document.createElement("td");
questionCell.className = "question";
row.appendChild(questionCell);
var rowCount = tableBody.rows.length;
var enumeratorContent = document.createTextNode(rowCount);
enumeratorCell.appendChild(enumeratorContent);
var questionText = textarea.value;
var questionContent = document.createTextNode(questionText);
questionCell.appendChild(questionContent);
你的html应该是这样的:
<table id="qandatbl" align="center">
<thead>
<tr>
<th id="qidhead">Question No</th>
<th id="questionhead">Question</th>
</tr>
</thead>
<tbody>
<?php
$spinnerCount = $_POST['textQuestion'];
if ($spinnerCount > 0) {
for($i = 1; $i <= $spinnerCount; $i++) {
?>
<tr>
<td class="qid"><?php echo $i; ?></td>
<td class="question"></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
<!-- table which consists of textarea and submit button -->
<form id="enter" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<table id='middleDetails' border='1'>
<tr>
<th class='tblheading' colspan='2'>SESSION DETAILS</th>
</tr>
<tr>
<td id="questionNum">Question No </td>
</tr>
<tr>
<td id="questionContent">Question:</td>
<td id="questionTextareaCell"><textarea id="questionTxt" rows="5" cols="40" name="questionText"></textarea></td>
</tr>
<tr>
<td id="addQuestionRow" colspan='2'><input id="addQuestion" type="button" value="Add Question" name="addQuestionBtn" onClick="insertQuestion()" /></td>
</tr>
</table>
</form>
您不应该为生成的单元格多次使用id,因为每个文档的id应该是唯一的。
最后一行试试:
questionCell.innerHTML = getQuestion.value;