简体   繁体   中英

Be able to insert text from a textarea into a table

This is what I want to know. I have a table which has the number of rows depended on what the number is in the spinner. This does work eg If I enter 25 in spinner it comes up with 25 rows, if I enter 7 in spinner comes with 7 rows.

So my problem is this:

Let's say there are a number of rows in a table. What I have is a textarea where the user enters in their question and then submits the question, the question should be inserted and appear in the first row of the table under the "Question" column, the textarea goes blank and the user enters in his second question, if the user submits this then the question would appear in the second row, 3rd question into 3rd row, 4th question 4th into row etc.

Problem is that I do not know how to do this. Can somebody please be able to show me how to achieve this. I am not a strong Javascript programmer, I am more a of an Oracle and MYSQL programmer but I need to use Javascript for my project.

Any help will be greatly appreciated

Below is my Javascript code:

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

Below is html code:

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

You first will have to create row and cell elements, before you can add them to the table.

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

Your html should look like this:

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

You should not use ids for cells your generating more than once, because an id should be unique per document.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM