Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从控制台用JavaScript填充表单_Javascript_Scripting_Console_Textarea - Fatal编程技术网

从控制台用JavaScript填充表单

从控制台用JavaScript填充表单,javascript,scripting,console,textarea,Javascript,Scripting,Console,Textarea,假设我有一个表单,其中有一个文本字段需要添加。字段重复100次 ... <textarea class="typeInStupid" data-text-type="execution-notes" placeholder="Notes go here..." rows="6" id="123"></textarea> ... ... <textarea class="typeInStupid" data-text-type="execution-notes"

假设我有一个表单,其中有一个文本字段需要添加。字段重复100次

...
<textarea class="typeInStupid" data-text-type="execution-notes" 
placeholder="Notes go here..." rows="6" id="123"></textarea>
...
...
<textarea class="typeInStupid" data-text-type="execution-notes" 
placeholder="Notes go here..." rows="6" id="124"></textarea>
...
。。。
...
...
...
任务:使用浏览器控制台和JavaScript向所有文本字段添加相同的文本块(f.e.“BlaBla”),以便:

...
<textarea class="typeInStupid" data-text-type="execution-notes" 
placeholder="Notes go here..." rows="6" id="123">BlaBla</textarea>
...
...
<textarea class="typeInStupid" data-text-type="execution-notes" 
placeholder="Notes go here..." rows="6" id="124">BlaBla</textarea>
...
。。。
布拉布拉
...
...
布拉布拉
...
其他有效的解决方案-请选择。:)
先谢谢你

解决方案是找到所有textarea字段,然后遍历每个字段并修改其值

var textFields = document.querySelectorAll(".typeInStupid");
for (var i = 0; i < textFields.length; i++) {
    textFields[i].value = "BlaBla";
}

如中所示,您不应该使用.getElementsByClassName,因为它不支持IE8。如果您直接使用JavaScript,那么应该将其替换为.querySelectorAll。IE8支持这一点。或者,使用jQuery可以让生活变得更加简单;这似乎有点简单。:)
$(".typeInStupid").each(function(){
    $(this).val("BlaBla");
});