What is the jQuery equivalent for document.forms [0] .elements [i] .value ;?

What is equivalent jquery: document.forms[0].elements[i].value; ?

I don't know how to get through the form and its elements in jQuery and would like to know how to do it.

+6
javascript jquery equivalent
source share
5 answers

Normal translation - :input selector:

 $("form:first :input").each(function() { alert($(this).val()); //alerts the value }); 

:first is that your example pulls out the first <form> , if only one or you want all the input elements, just take :first off. The :input selector works for <input> , <select> , <textarea> ... all the elements that you usually like.

However, if we knew exactly what your goal was, perhaps this is a very simple way to achieve this. If you can post more information like HTML and what values ​​you want to extract (or do something else with).

+13
source share

Well, literally translated, it will be:

 $('form:first *:nth-child(i)').val() 

But jQuery makes it easier to capture elements in other ways, such as an identifier or CSS selector. It would be easier to maintain if you did something like:

 $('form#id input.name').val() 
+1
source share

I'm not quite sure what you are trying to execute, but you should do something like this:

 $('form:first').children(':first').val(); 

This will get the value of the first child node within the first <form> in the DOM.

0
source share
 $("#formid input").each(function(){ alert($(this).attr("value")) }) 
0
source share

This will give you all the elements in the form. Including formless elements:

 $("#[form id]").find() 

Then you can use each function to move all the children. Or you can use the input selector only to return form elements:

 $("#[form id] :input") 
0
source share

All Articles