模态中的表单不提交jquery

时间:2016-09-05 11:46:54

标签: javascript jquery html forms bootstrap-modal

我试图创建一个使用条带的模态,但我无法提交表单。我不知道为什么jQuery似乎无法引用表单id。当我引用表单ID时,代码似乎还没有识别它,当我引用父div并使用.click引用表单时,它似乎引用了表单。这是一个怪癖jQuery还是我有些愚蠢的实现

<!-- 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>

从上面可以看出,我有#cardModal(div) - &gt; #updateCard(表格) - &gt; #submitP(提交)。我不能引用updateCard,除非我使用$('#cardModal').on('click', 'updateCard')这样的东西,但这不是我想要的,因为我只想在用户按下提交时访问该表单。我想要这个的原因是因为我想在下面的表单中添加一个条带语句

// 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();
    }
}

1 个答案:

答案 0 :(得分:3)

检查FORM声明U将其称为FROM not FORM ..因此它不会调用提交功能将其更改为...

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

像这样改变:

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