Loop through onchange input elements using jQuery

When the selection box is changed, I would like to see all the input values ​​in a form owned by jQuery. This does not work below, but I can’t determine exactly how to do this.

$("select.mod").change(function(){
    $(this).parent().get(0).$(":input").each(function(i){
    alert(this.name + " = " + i);
  });
});
+3
source share
2 answers

This is probably the choice of the "parent form" that causes the problem.

The function .parent()returns only the immediate parent element, which will not allow you to get a form element if yours is select.modnested in <p>or something like that.

The function .parents()returns all the parents of the element; but the first may not be a form tag. I would try this:

$("select.mod").change(function(){
    $(this).parents('form') // For each element, pick the ancestor that a form tag.
           .find(':input') // Find all the input elements under those.
           .each(function(i) {
        alert(this.name + " = " + i);
    });
});

, , , , , , , jQuery...

+9

:

.$(":input")

. $(":input") .get(0)... , !

, , , :

$(this).parent().find(":input").each( ... )

, . ? DOM? Etc.

id . clea (r | n) er :

$('#my_awesome_form :input').each( ... )
+2

All Articles