I have a form with two fields, which I try to submit using ajax. I have jquery form validator plugin, which works fine with all other forms. When I try an ajax submission, the validation doesn't seem to be working. The sacript I use is:
$("#add-edit-template").click( function(event)
{
var x;
x = $('.sp-preview-inner').css('backgroundColor');
if(x)
{
hexc(x);
$('#addEditTemplateForm').append("<input type='hidden' name='templateColor' value='"+ color+"' />");
}
$("#addEditTemplateForm").submit(function(e)
{
e.preventDefault();$("div#divLoading").addClass('show');
var postData = $("#addEditTemplateForm").serializeArray();
var formURL = $(this).attr("action");
$.ajax(
{
url : formURL,
type: "POST",
data : postData,
dataType : "html",
success:function(htmlData)
{
$('#ph_widgets').replaceWith(htmlData);
$("#templateSuccess").show();
$("#phButtons").show();
$('#listWidgets').dataTable({"iDisplayLength" : 25, "aaSorting": []});
initDraggabletable("#selectedWidgetTable");
},
error: function( xhr, status, errorThrown )
{
console.log( "Error: " + errorThrown );
console.log( "Status: " + status );
console.dir( xhr );
},
});
$("div#divLoading").removeClass('show');
e.preventDefault(); //STOP default action
});
});
I need the click function since I am taking a value from a div, which is also required for me . I tried to add
$.validate
({
form : '#addEditTemplateForm'
})
Immediately after the click and also before the ajax call , but the submission is not getting prevented and the validation is not preventing that. I found may solutions online, but nothing worked for me. Is there any way to fix this...Thanks in advance