简体   繁体   中英

Either Can Not submit(); form in jQuery, or can not check if input fields are filled out

I am having trouble submitting the below form.

For background, I'm trying to "submit" a form for a delivery, and I need to know a) their pickup address, b) their dropoff address, and c) their description. I created <p class="error"> fields if those <input> s are empty (as in "Please enter a description").

If I remove the 'return false;' the form submits no matter what, but if I keep the 'return false;' the jQuery works (ie - error message appears) but now the form NEVER submits. Thoughts?

Here's my main.js

var main = function() {
  $('form').submit(function() {
    var pickup = $('#pickup').val();

    if(pickup === "") {
      $('.pickup-error').text("Please choose a pickup.");
    }



    var dropoff = $('#dropoff').val();

    if(dropoff === "") {
      $('.dropoff-error').text("Please choose a dropoff.");
    }

    var description = $('#description').val();

    if(description === "") {
      $('.description-error').text("Please tell us a little about what we're moving.");
    }

      return false;
  });
};

$(document).ready(main);
   var main = function () {
        $('form').submit(function () {

            var pickup = $('#pickup').val();
            if (pickup === "") {
                $('.pickup-error').text("Please choose a pickup.");
            }

            var dropoff = $('#dropoff').val();
            if (dropoff === "") {
                $('.dropoff-error').text("Please choose a dropoff.");
            }

            var description = $('#description').val();
            if (description === "") {
                $('.description-error').text("Please tell us a little about what we're moving.");
            }

            // did not pass validation
            if (pickup != "" || dropoff != "" || description != "") {
                return false;
            }

            // passed validation, submit
            return true;
        });
    };

    $(document).ready(main);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM