Reputation: 47
I have several forms in a page and in each form a submit. But it is not working because this return me the two forms. Someone know to do it?
Thanks
$(".submit").click(function() {
processBeforeSend($(".submit").closest("form"));
});
function processBeforeSend(form) {
form.each($('input'), function() {
if ($(this).attr('class') == 'integer'){
validateInteger(field);
}
else{
if (filedClass == 'radio'){
validateRadio(field);
}
else{
}
}
});
Upvotes: 1
Views: 44
Reputation: 24638
Instead of using the submit
button's click event, I would just let the submit
button do it's job -- trigger the form's submit
event:
$("form").on( 'submit', processBeforeSend );
function processBeforeSend() {
//'this' in here refers to the form whose submit event was triggered.
$(this).find('input').each(function(i, field) {
//'field' was not defined but in here it equals 'this'
//and refers to the current input element
//An element can have more than one class so $(this).attr('class') is not the way to go.
if( $(this).hasClass('integer') ){
validateInteger(field);
} else if( $(this).hasClass('radio') ) {
validateRadio(field);
} else {
}
});
}
Upvotes: 1
Reputation: 93561
Using click
instead of submit
means that keyboard submission will bypass your code! Always use the submit
event instead for forms. If you use the submit
event your this
is the form
:)
Also, your validation probably requires the form to "not" submit if validation fails. So pass the event object to your validation and call e.preventDefault()
if any validation fails.
e.g.
$(".submit").submit(function(e) {
processBeforeSend($(this), e);
});
function processBeforeSend(form, e) {
// validation calls
if (somethingFails){
e.preventDefault()
}
});
Upvotes: 1
Reputation: 388316
You need to find the form
which contains the clicked .submit
element, for that you can use this
to refer to the clicked .submit
element inside the click handler
$(".submit").click(function() {
processBeforeSend($(this).closest("form"));
});
Upvotes: 0