[英]Be able to insert text from a textarea into a table
这就是我想知道的。 我有一个表,其中的行数取决于微调器中的行数。 这确实有效,例如,如果我在微调器中输入25,则显示25行,如果我在微调器中输入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>
尝试此作为最后一行:
questionCell.innerHTML = getQuestion.value;
首先,您必须先创建行和单元格元素,然后才能将它们添加到表中。
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在每个文档中都应该是唯一的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.