开发者

Javascript-HTML - how to iterate through all the forms on a page?

How can I iterate through all forms in a docume开发者_如何学Pythonnt using javascript?


You can use

document.forms collection

See forms Collection


The code below will go through an html document, get all forms and do a pop-up alert of the names of each form.

var formsCollection = document.getElementsByTagName("form");
for(var i=0;i<formsCollection.length;i++)
{
   alert(formsCollection[i].name);
}

This is just a start to see if you are getting the reult you require. Thereafter, remove the alert and continue to do what you need to.


Here's an example using the document.forms instead of getElementsByTagName().

As with the getElementsByTagName() example this goes through all the forms and does a popup alert with the action (instead of name, as it is more likely to be set).

var formsCollection;
var r;

formsCollection=document.forms;

for(r=0;r<formsCollection.length;r++)
{
    alert(formsCollection[r].action);
}

This can be condensed down and of course the popup changed to something useful but I have tried to keep it simple.

And for reference here are some links to more info:

  • Mozilla Developer Network
  • Microsoft
  • w3c
  • And even w3schools
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜