gpt4 book ai didi

javascript - ajax点击功能可以在提交表单时起作用吗?

转载 作者:行者123 更新时间:2023-12-02 14:23:06 25 4
gpt4 key购买 nike

我正在做一个网站的前端,我需要在提交表单的同时显示一个弹出模式。我需要使用 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>

最佳答案

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

小例子是:

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

关于javascript - ajax点击功能可以在提交表单时起作用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38520506/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com