Skip to content Skip to sidebar Skip to footer

Jquery Submit Function Working Sometimes

I'm making a web application for a class which contains a jsp file that uses jquery. I want the app to trigger an alert right before submitting. This works some of the time in my r

Solution 1:

In your example, answers is a jQuery collection of elements. Looping through it using for(var obj in answers) { } is actually looping through the properties of answers, not the elements themselves. Therefore, calling .attr() on a property is not going to work.

In general, if I see my debugger state that jQuery has an error, it's 99.9% of the time me calling a jQuery method on a non-jquery selected object. In this case, I saw the error in Chrome's JavaScript console, and sometimes results may vary with different consoles.

A good practice is to prefix variables that store jQuery elements with $ to indicate that they are jQuery objects. For instance, $answers makes it easier to keep track of what it contains.

Use:

answers.each(function() {
     answer += $(this).attr("value") + "#";
});

Instead of this:

for (var obj in answers) {
    answer += obj.attr("value") + "#";
}

Post a Comment for "Jquery Submit Function Working Sometimes"