I have a form that submits by javscript's submit() function only (no button or mouse click) and I am trying to add an attribute to it using jquery when this submission is activated. Since the code is lengthy, I am including only the form code itself, the javascript line that triggers the submission, the jquery code that is being used (it creates a message box warning the user that they are about to leave the page and the jquery that I have written that does not work. The jquery intent is to create the message box but also create jquery attributes to eliminate the need for the box such as when submitting the form and such.
The form code is as follows:
<form id='phpvalue' name='phpvalue' action='timedel.php' method="POST">
<input type='text' id='phpval' name='phpval' value="0" style="width:16px">
</form>
The javascript code for the form is a countup sequence that ends with the following javascript line that submits the above form:
document.getElementById("phpvalue").submit();
The jquery code that I currently have (which I picked up from another jquery answer and am modifying
it to fit the situation) now is as follows:
<script type='text/javascript'>
window.onbeforeunload = function() {
return "You're leaving the site.";
};
$(document).ready(function() {
$('form').submit(function() { window.onbeforeunload = null; });
$('form#phpvalue').triggerhandler("submit", function() { window.onbeforeunload = null; });
});
</script>
I have highlighted the line that is the problem. I have tried the jquery line:
$('form#phpvalue').on("submit", $(function() { window.onbeforeunload = null; }));
which works but it works on all items (does not allow the message box to popup period) which sort of makes the entire effort worthless. I need it to do just the opposite (message box to popup all the time with the exceptions created through jquery ("null"). Any help would be greatly apprciated. Thanks in advance.