【问题标题】:Be able to insert text from a textarea into a table能够将文本区域中的文本插入表格
【发布时间】:2011-11-28 02:19:47
【问题描述】:

这是我想知道的。我有一个表,其行数取决于微调器中的数字。这确实有效,例如如果我在微调器中输入 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>

【问题讨论】:

    标签: javascript button insert html-table textarea


    【解决方案1】:

    您首先必须创建行和单元格元素,然后才能将它们添加到表格中。

    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 应该是唯一的。

    【讨论】:

    • 我会试试这个,谢谢你,我会回复你并说明这会带来什么结果
    • 它说 table.bodies 是未定义的,你知道它应该是什么吗?
    • 糟糕,应该是table.tBodies
    • 您好,感谢您的尝试,遗憾的是它没有奏效。当我提交问题时,它根本不会在“问题”列下的表格中显示问题
    • 对。我混淆了var row = tableBody.insertRow(tableBody.rows.length);var row = document.creatElement("tr"); tableBody.appendChild(row);。抱歉,上面已修复。
    【解决方案2】:

    在最后一行试试这个:

    questionCell.innerHTML = getQuestion.value;
    

    【讨论】:

    • 嗨,我有两个问题,一个是每次我提交一个问题时,它都表示它是未定义的,并且它也没有显示在问题列中。如何做到这一点
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-06
    • 2016-09-29
    • 1970-01-01
    • 2013-04-14
    • 1970-01-01
    相关资源
    最近更新 更多