简体   繁体   中英

Bootstrap 3 modal form not triggering jQuery .submit()

I am trying to get a Bootstrap 3 modal form to trigger a form.submit() in jQuery, but no matter what I try, I can't get it to fire .

<div class="modal fade" id="modal-signup" tabindex="-1" role="dialog" aria-labelledby="modal-signup" aria-hidden="true">
  <div class="modal-dialog modal-sm">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title">Sign up</h4>
      </div>
      <div class="modal-body">
        <form role="form" action="/"  id="modal-signup-form" >
          <div class="form-group">
            <input type="email" class="form-control input-lg" placeholder="email">
          </div>
          <div class="form-group">
            <input type="password" class="form-control input-lg" placeholder="password">
          </div>
          <div class="form-group">
            <input type="password" class="form-control input-lg" placeholder="confirm">
          </div>
        </form>
      </div>
      <div class="modal-footer">
        <p>Already have account ? <a href="#" data-toggle="modal" data-target="#modal-signin">Sign in here.</a></p>
        <input type="submit" class="btn btn-success btn-block btn-lg" value="Sign up">
      </div>
    </div>
  </div>
</div>

and the JS

$("#modal-signup-form").submit(function( event ) {

event.preventDefault();

  alert("made it");

    $.ajax({
        type: "POST",
        url: "adduser.php",
        data: $('form.adduser_model').serialize(),
        success: function (msg) {

            $("#thanks").html(msg)
            $("form.noteform").modal('hide');
        },
        error: function () {
            alert("failure");
        }
    });
    return false;
});

I've created a fiddle...

http://jsfiddle.net/menriquez/sreco3jt/

If you hit "Sign Up" and "Sign Up" in the model form, I would expect the event to fire and see the alert, but it doesn't.

It's because the submit button isn't inside the #modal-signup-form form element.

Either add the button to the inside of the <form> , or add an event handler to trigger a submit when the button is clicked:

$(document).on('click', '#modal-signup-form-submit', function (e) {
    $('#modal-signup-form').submit();
});

Updated Example

You have to add following submit button code before closing of form tag.

<input type="submit" class="btn btn-success btn-block btn-lg" value="Sign up">

JS Fiddle

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