【问题标题】:Will ajax click function work in submitting a form?ajax 点击功能在提交表单时会起作用吗?
【发布时间】:2016-11-26 00:09:48
【问题描述】:

我正在做一个网站的前端,我需要在提交表单的同时显示一个弹出模式。我需要使用 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 能正常工作并提交表单吗?” - 你尝试的时候发生了什么?
  • 它会起作用,但这不是一个好习惯。
  • @nnnnnn 我只在前端工作,无法处理后端问题。

标签: javascript jquery html ajax forms


【解决方案1】:

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

小例子是:

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

【讨论】:

  • 所以$("#button").click(function() {... 应该可以工作吗?因为在 ajax 示例中,我看到的是 .submit(function(){...
  • 谢谢。提交后还会显示我放的模态框吗?
  • 如果要在提交后显示模型,请将其放在按钮点击事件中'
  • 我刚刚开始学习 AJAX。现在可以了,对吧?提交我的表单并显示模态:$("#myButton").click(function() { var $form = $("#myForm"), url = $form.attr('action'); $.ajax({ url: url, type: "POST", data: $form.serialize() }); modal.style.display = "block"; });
  • 是的,但是在点击事件之前声明模型。表示 put var modal = document.getElementById('myModal');在点击事件之前
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-06
相关资源
最近更新 更多