Hello guys, I'm developing an application that uses Jquery to intercept my form submissions to make instead an Ajax Submission. So far so good! Everything works great.. If the client doesn't have JS enabled, the app keeps working flawless.
Well, today I created a new form but JQuery doesn't intercept the submission. It's true that I changed a little bit but still. It should be working.
I have a simple PartialView with the form, a span and two buttons (NO (button) & YES (submit)). I use RenderPartial("MyPartial") into a DIV and the plugin FancyBox to display it as a modal. Depending on which link a click to show the modal, I change the action (report spam or delete item) of the form and the span with a certain msg. Like a "fancy" confirmation message!!
Again, so far so good. I change the form action and it works (using alert(form.attr(action)), I change the span with the right message and it works, but when I click on the button to submit the form (YES), it goes straight and I make a normal request, and not a Ajax request and I wanted.
This is the form:
<form id="dialogForm" action="" method="post">
<div class="form_item_button">
<span id="dialogQuestion" style="color: Black;"></span>
<button id="btDialogNo" type="button" value="button" class="btn">No</button>
<button id="btDialogYes" type="submit" value="Submit" class="btn">Yes</button>
</div>
</form>
And this is the script to intercept the form submission:
$(document).ready(function() {
var theForm = $("#dialogForm");
// I CHECK IF THE ACTION WAS SET PROPERLY
// MY TESTS SHOW ME THAT THE ACTION IS OK!
alert(alert(theForm).attr("action"));
theForm.submit(function(e) {
e.preventDefault();
$.ajax({
type: "POST",
data: theForm.serialize(),
url: theForm.attr("action"),
dataType: "json",
success: function(json) {
alert(json.Status);
},
error: function() {
alert('An error occurred while processing your request. Please try again.');
}
});
});
Any idea guys??? I lost my entire day trying to fix it!! :(
Thanks