从控制台用JavaScript填充表单
假设我有一个表单,其中有一个文本字段需要添加。字段重复100次从控制台用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"
...
<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");
});