0

This is a fairly basic survey form; I'm using JQuery to conditionally show/hide fields based on user selections.

//FORM CONDITIONALS Hides all the helpfulness radios and only shows them when "Yes" for a particular question is checked
$("#datainp .occurrence").click(function () {
    var nexttd = $(this).next('td');
    if ($(this).children("input:first").is(':checked')) {
        $(nexttd).show();
        $(nexttd).children("input").removeAttr("checked");
    } else {
        $(nexttd).hide();
    }

I have a client-side validation function as well:.

//Client side validation and submissions
$('#submit').click(function(e) {

    e.preventDefault();

    var level = $('#Level').val();
    var schoolid = $('#school').val();

    if (!schoolid) {
        alert("Please select a school!");
        return;
    }
    if (!level) {
        alert("Please select a grade level!");
        return;
    }
    alert("foo");
    $('#frmTeacherEvalGuidance').submit(function() {alert("bar") });

});

Everything works EXCEPT the .submit() at the end of my validation (alerts verify). Is something in my form conditional section conflicting with .submit()? When I comment out the first section of javascript, the form submits as it's supposed to. JsFiddle is here: http://jsfiddle.net/r4Y4g/1/ (The html is a tad clunky, I know, but not mine; updating an old team project)

WillardSolutions
  • 2,316
  • 4
  • 28
  • 38

2 Answers2

1

You should use .stopPropagation() instead of .preventDefault()

Try this:

//Client side validation and submissions
    $('#submit').click(function(e) {

        e.stopPropagation();

        var level = $('#Level').val();
        var schoolid = $('#school').val();

        if (!schoolid) {
            alert("Please select a school!");
            return false;
        }
        if (!level) {
            alert("Please select a grade level!");
            return false;
        }

        return true;   
    });
Renato Galvones
  • 533
  • 1
  • 3
  • 14
0

You could add a listener instead:

$("#frmTeacherEvalGuidance").on("submit", function () {
   alert("bar"); 
});
steve
  • 381
  • 3
  • 4
  • Just tried rewriting function as listener on the submit as suggested, but that means either 1) I loose the ability to validate (ie it submits regardless of my if statements); or 2) if I use preventDefault I still have to use a .submit() after my validation. In either case, I still don't have the functionality I need. – WillardSolutions Feb 20 '14 at 20:39