I have a form in sidebar on few pages of my website
<form id="test1" name="test1">
<label for="fname">First Name:</label>
<input type="text" name="fname" id="fname" />
<label for="lname">Last Name:</label>
<input type="text" name="lname" id="lname" />
<label for="comment">Comment:</label>
<textarea name="comment" id="comment"><textarea/>
<input type="submit" />
</form>
My requirement is something odd. If someone has filled up the form (even partially) and then leaving current page (by going to some other page, or closing browser tab/ window), then form should be submitted.
I am putting following jQuery code to achieve following:
<script>
$(window).bind('beforeunload', function(){
$('form').each(function(index) {
var filled = false;
$(this).find("input").each(function() {
if ($(this).val() != "") {
filled = true;
}
});
if (filled) { this.submit(); }
});
return confirm('test');
});
Above code is not working properly, form is not getting submitted. Help please.