JavaScript for循环来更改函数中textareas的名称 [英] Javascript for loop to change the name of textareas in a function

查看:176
本文介绍了JavaScript for循环来更改函数中textareas的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了50 textareas ,名称为 def1,def2,def3 .....,def50 。在我的身体 onLoad()函数中,我希望在所有这些文本框中设置相同的值。



写代码50次,我怎样才能写一些Javascript代码来设置 textarea 的值,即在一个循环中?



<$ p

($ i $ 0; i <50; i ++)
document.getElementsByName(def+ i)[0] .value函数loader(){
='任何值';
}


I have created 50 textareas with names def1,def2,def3.....,def50. In my body onLoad() function,I want the same value is set in all these textboxes.

Instead of writing the code 50 times, How can I write some Javascript code to set the value of the textarea, ie in a loop?

解决方案

You can use tagname property but it will not work if you have some more textbox anywhere else in your page

 function loader(){
    for(var i=0;i<50;i++)
    document.getElementsByName("def"+i)[0].value='Any Value';
    }

这篇关于JavaScript for循环来更改函数中textareas的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆