简体   繁体   中英

form in modal not submitting with jquery

Im trying to create a modal for using stripe in but I cant get the form to submit. I don't know why jQuery cant seem to reference the form id. When I reference the form id I the code does not seem to recognise it yet when I reference the parent div and use a .click referencing the form it seems to reference the form then. Is this a quirk yet jQuery or have I some silly implementation

<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-default btn-lg" data-toggle="modal" data-target="#cardModal">Update card Details</button>
<!-- 'Update' Modal -->
<div id="cardModal" class="modal fade" role="dialog">
   <div class="modal-dialog">
        <!-- Modal content-->
        <div class="modal-content">
            <!-- Modal header -->
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title">Update card details</h4>
            </div>
                <!-- Modal body -->
                <div class="modal-body">
                    <from action="/settings/card" method="post" id="updateCard" class="form-horizontal">
                    <div class="form-group has-feedback">
                        <label class="control-label col-sm-2">Card Number:</label>
                         <div class="col-sm-6">
                              <input type="text" class="form-control" data-stripe="number" required>
                               <i class="form-control-feedback glyphicon glyphicon-pencil"></i>
                         </div>
                    </div>
                    <div class="form-group has-feedback">
                        <label class="control-label col-sm-2">Name on card:</label>
                        <div class=" col-sm-6">
                            <input type="text" class="form-control" data-stripe="name" required>
                             <i class="form-control-feedback glyphicon glyphicon-user"></i>
                        </div>
                    </div>
                    <div class="form-group has-feedback">
                        <label class="control-label col-sm-2">Expiry Date:</label>
                        <div class="col-sm-4">
                            <div class="form-inline">
                                <div class="form-group">
                                     <input type="text" class="form-control" data-stripe="exp_month" placeholder="Month" required/>
                                     input type="text" class="form-control" data-stripe="exp_year" placeholder="Year" required />
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="form-group has-feedback">
                        <label class="control-label col-sm-2">CVC:</label>
                        <div class=" col-sm-2">
                             <input type="password" class="form-control" data-stripe="cvc" required>
                             <i class="form-control-feedback glyphicon glyphicon-shopping-cart"></i>
                        </div>
                    </div>
                    <!-- Form Submit -->
                    <button type=submit id="submitP" class="btn btn-default btn-lg">Submit</button>
            </from>
        </div>
        <!-- Modal footer -->
        <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Exit</button>
        </div>
    </div>
</div>

As can be seen above I have the #cardModal(div) -> #updateCard(form) -> #submitP(Submit). I cannot reference the the updateCard unless i Use something like $('#cardModal').on('click', 'updateCard') but this is not what I want because I just want to access the form when the user presses submit. Th reason I want this is because I want to add a stripetoken to the form which can be seen below

// Before the form submits
$('#updateCard').submit(function(event){
    alert('submitted');
    //Disable the button
    $('#updateCard').find('.btn btn-default btn-lg').prop('disabled', true);
    //Create a token for the user
    Stripe.card.createToken($('#updateCard'), stripeResponseHandler);
    // Prevent the form from being submitted:
    return false;
});

function stripeResponseHandler(status, response) {
    console.log('in response handler')
    // Grab the form:
    var $form = $('#cardModal updateCard');
    //If there is an error
    if (response.error) { // Problem!
        console.log(response.error)
        // Show the errors on the form:
        $('<div id="paymentError" class="alert alert-danger">'+response.error.message+'</div>').insertBefore('#submit');
        $('#updateCard').find('.btn btn-default btn-lg').prop("disabled", false ); // Re-enable submission
    } else { 
        // Token was created!
        console.log('created');
        // Get the token ID:
        var token = response.id;
        // Insert the token ID into the form so it gets submitted to the server:
        $form.append($('<input type="hidden" name="stripeToken">').val(token));
        // Submit the form:
        $form.submit();
    }
}

CHECK The FORM Declaration U mentioned it as FROM not FORM..so it won't invoke submit function change it to form..

                <from action="/settings/card" method="post" id="updateCard" class="form-horizontal">

Change it like this:

                <form action="/settings/card" method="post" id="updateCard" class="form-horizontal">

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