动态地将变量添加到html表

时间:2015-04-25 16:58:04

标签: javascript html

我在这里用html创建了一个表。

<tr id='addr0'>
    <td>1</td>
    <td>
        <input type="text" id='word0' placeholder='Word' 
               value="Abhor" class="form-control" />
    </td>
    <td>
        <input type="text" id='definition0' placeholder='Definition' 
               value="regard with disgust and hatred." class="form-control" />
    </td>
    <td>
        <input type="text" id='synonym0' placeholder='Synonyms' 
               value="detest, hate" class="form-control" />
    </td>
</tr>
<tr id='addr1'></tr>

如何使用java脚本在文本区域中添加一个或多个变量。我环顾四周,找不到任何代码。

这是我尝试过的:

<script>
    document.getElementById("synonym0").innerHTML = "Test";
</script>

2 个答案:

答案 0 :(得分:1)

输入元素&#39;内容由value属性访问,而不是innerHtml。尝试:

document.getElementById("synonym0").value = "Test";

答案 1 :(得分:0)

我认为你的问题在这里得到解答:How to change the Content of a <textarea> with Javascript

但无论如何这里是snippet

// to set the content to a variable (an string):

document.getElementById('word0').value = 'Testing!';

var arr = ['member 1', 'member 2', 'member 3'];

// To set the content to an array:

document.getElementById('definition0').value = arr.toString();
<tr id='addr0'>
    <td>1</td>
    <td>
        <input type="text" id='word0' placeholder='Word' value="Value" class="form-control" />
    </td>
    <td>
        <input type="text" id='definition0' placeholder='Definition' value="Default value" class="form-control" />
    </td>

</tr>
<tr id='addr1'></tr>