ajax点击功能会在提交表单时起作用吗?

时间:2016-07-22 07:23:04

标签: javascript jquery html ajax forms

我正在做一个网站的前端,我需要在提交表单的同时显示一个弹出模式。我需要使用INPUT TYPE =“button”并使用JQUERY CLICK函数而不是SUBMIT。此代码中的ajax是否可以工作并提交表单?

$("#submit_cc").click(function() {
        $.ajax({
            url: "#",
            type: "POST",
            data: {
                'acctNumber': $('#acctNumber').val()
            }
        });modal.style.display = "block";
    });
/* Display popup modal */
// Get the modal
var modal = document.getElementById('myModal');

// Get the button that opens the modal
//var btn = document.getElementById("submit_cc");

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("span_cc_btn")[0];

// When the user clicks the button, open the modal
/*btn.onclick = function() {
    modal.style.display = "block";
}*/

// When the user clicks on <span>, close the modal
span.onclick = function() {
    modal.style.display = "none";
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
    if (event.target == modal) {
        modal.style.display = "none";
    }
}
/* The Modal (background) */

.modal {
    display: none;
    /* Hidden by default */
    
    position: fixed;
    /* Stay in place */
    
    z-index: 1;
    /* Sit on top */
    
    padding-top: 100px;
    /* Location of the box */
    
    left: 0;
    top: 0;
    width: 100%;
    /* Full width */
    
    height: 100%;
    /* Full height */
    
    overflow: auto;
    /* Enable scroll if needed */
    
    background-color: rgb(0, 0, 0);
    /* Fallback color */
    
    background-color: rgba(0, 0, 0, 0.4);
    /* Black w/ opacity */
}
/* Modal Content */

.modal-content {
    font-family: Open Sans;
    color: #afafaf;
    font-size: 16px;
    position: relative;
    background-color: #fefefe;
    border: 1px solid #888;
    box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
    -webkit-animation-name: animatetop;
    -webkit-animation-duration: 0.4s;
    animation-name: animatetop;
    animation-duration: 0.4s;
    text-align: center;
    width: 500px;
    margin: auto;
    border-radius: 0px;
    padding: 30px;
}
.modal-content button {
    font-family: Roboto;
    font-size: 20px;
    font-weight: normal;
    color: white;
    background-color: #ea6a1d;
    border: none;
    padding: 8px 50px;
    text-align: center;
    border-radius: 6px;
}
/* Add Animation */

@-webkit-keyframes animatetop {
    from {
        top: -300px;
        opacity: 0
    }
    to {
        top: 0;
        opacity: 1
    }
}
@keyframes animatetop {
    from {
        top: -300px;
        opacity: 0
    }
    to {
        top: 0;
        opacity: 1
    }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="frm_payt_cc">
	<h2>Payment</h2>

	<label class="bill_label">Account Number</label>

	<p><input type="number" name="acctNumber" class="form-control bill_value col-sm-12 col-xs-12" placeholder="Enter acct num"></p>
	
	<br><input type="button" id="submit_cc" class="bill_btn col-lg-7 col-md-7 col-sm-12 col-xs-12" value="Submit Payment &rarr;">
</form>

<!--  MODAL POP UP -->
<div id="myModal" class="modal">
	<!-- Modal content -->
	<div class="modal-content">
		<div class="bill_popup modal-body">
			<h2>Thank You!</h2>
			<br>
			<center><span class="span_cc_btn"><button type="button">Okay</button></span></center>
		</div>
	</div>
</div>

1 个答案:

答案 0 :(得分:2)

是的,您绝对可以在POST中使用ajax方法发布所有表单内容,因此您无需提交表单,如果没有提交,您可以使用ajax将数据发布到php。

小例子是:

 $("#submit_cc").click(function(event){
    var $form = $("#testform"),
    url = $form.attr( 'action' );
    $.ajax({
        url: url,
        type: "POST",
        data: $form.serialize()
    });
 });