假设您的 HTML 如下
<textarea id="text_area">
Line 1
Line 2
Line 3
</textarea>
<div id="input_text"></div>
此 Javascript 将根据您的文本区域的内容创建输入元素
// Destination element to contain the input elements
var destination = document.getElementById('input_text');
// Contents of textarea
var content = document.getElementById('text_area').innerHTML;
// Array containing each line of the textarea
var lines = content.split('\n');
for(i = 0; i <= lines.length; i++)
{
if(lines[i] != '' && lines[i] != undefined)
{
// Create input element
el_name = 'input_' + i;
el = document.createElement('input');
el.setAttribute('type', 'text');
el.setAttribute('name', el_name);
el.setAttribute('value', lines[i]);
// Append input element to destination
destination.appendChild(el);
}
}
这里的工作示例http://fiddle.jshell.net/AvA3a/