admin管理员组文章数量:1406937
I would like to know if it is possible to loop through all the textboxes on a page and insert a value into each one starting with the number 1 and going up by +1. There are over 150 textboxes on this page and they are not in an array and I would not like to rename them one by one.. :-(
Thanks for any help....
I would like to know if it is possible to loop through all the textboxes on a page and insert a value into each one starting with the number 1 and going up by +1. There are over 150 textboxes on this page and they are not in an array and I would not like to rename them one by one.. :-(
Thanks for any help....
Share Improve this question asked May 27, 2011 at 21:42 lukeluke 131 silver badge3 bronze badges 1- "rename" or "set a value", what should it be? – Tomalak Commented May 27, 2011 at 21:49
3 Answers
Reset to default 5var input = document.getElementsByTagName("INPUT");
var j = 0;
for (var i = 0; i < input.length; i++) {
if (input[i].type == "text") {
input[i].value = ++j;
}
}
function FillTextBoxes()
{
var tbs = document.getElementsByTagName("input");
var valCount = 0;
for (var i = 0 ; i < tbs.length ; i++)
{
if (tbs[i].type == "text")
{
tbs[i].value = ++valCount;
}
}
}
window.onload = FillTextBoxes;
Try this out:
$('input[type="text"]').each(function(index) {
$(this).val(index);
})
Per the request of some, for pleteness' sake, the above requires the JavaScript library, jQuery. You can reference it like so:
<script src="//ajax.aspnetcdn./ajax/jQuery/jquery-1.6.1.js"></script>
If you aren't using <!DOCTYPE html>
you can include the type="text/javascript"
attribute.
本文标签: javascriptLoop through textboxes inserting values textboxes not in arrayStack Overflow
版权声明:本文标题:javascript - Loop through textboxes inserting values textboxes not in array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744664807a2618462.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论