Javascript追加Textarea值问题

时间:2016-11-13 09:56:24

标签: javascript append

我创建了四个textarea框,我想在单击提交按钮时获取它们的值。当我单击提交按钮时,我想附加文本,它是在提交按钮后以纯文本形式写在textareas中的。我试过^并附加值,但它不起作用。这是我到目前为止所做的。任何人都可以帮我找到问题所在?

感谢。

HTML代码:

[^io)sxz(c]

Javascript代码:

createElement("p")

2 个答案:

答案 0 :(得分:1)

cElementOne = firstContent.value; - 这会重写您的变量

试试此代码

var cElementOne = document.createElement("p");
cElementOne.innerText = firstContent.value;
customContainer.appendChild(cElementOne);

答案 1 :(得分:0)

根据您的JavaScript代码,判断第一个文本区域是否正确。

- 错误的 fisrt-content

textarea id="**fisrt**-content" class="content-area" placeholder="Insert Content Here"></textarea>

- 正确识别ID第一内容 <textarea id="first-content" class="content-area" placeholder="Insert Content Here"></textarea>

享受

下面是为我工作的正确代码,并确保首先在谷歌浏览器上运行,然后在其他浏览器中尝试,因为似乎addeventlister不支持Internet Explorer:

HTML

 ` <div class="content-container">
    <label>First Content<label>
      <textarea id="first-content" class="content-area" placeholder="Insert 
      Content Here"></textarea>
      </div>

      <div class="content-container">
        <label>Second Content<label>
      <textarea id="second-content" class="content-area" placeholder="Insert   
      Content Here"></textarea>
      </div>

      <div class="content-container">
        <label>Third Content<label>
      <textarea id="third-content" class="content-area" placeholder="Insert 
       Content Here"></textarea>
      </div>

      <div class="content-container">
        <label>Fourth Content<label>
         <textarea id="fourth-content" class="content-area"      
     placeholder="Insert        Content Here"></textarea>
      </div>


      <button id="c-btn" name="c-btn">Submit</button>

      <div id ="c-content"></div> 

   </div>`


`<script type="text/javascript">
var firstContent = document.getElementById("first-content");
var secondContent = document.getElementById("second-content");
var thirdContent = document.getElementById("third-content");
var fourthContent = document.getElementById("fourth-content");
var customContainer = document.getElementById("c-content");
    var submitBtn = document.getElementById("c-btn");


    function submitCustomForm() {

    var cElementOne = document.createElement("p");
    cElementOne = firstContent.value;
    customContainer.append(cElementOne);

    var cElementTwo = document.createElement("p");
    cElementTwo = firstContent.value;
    customContainer.append(cElementTwo);

    var cElementThree = document.createElement("p");
    cElementThree = firstContent.value;
    customContainer.append(cElementThree);

    var cElementFour = document.createElement("p");
    cElementFour = firstContent.value;
    customContainer.append(cElementFour);
    }

    submitBtn.addEventListener("click", submitCustomForm);
    </script>`

Sudeep Patel