按下按钮后"submit"表单如何从Ajax工作



我有表单,需要使用Ajax。当我在推送后发送电子邮件时一切都好";输入";在键盘上。当我想使用";按钮";在HTML中它是不起作用的。我尝试使用";点击";我的JS文件中的按钮,但它刷新了页面,我只在空页面上收到消息(自定义消息(。

<form action="{{ route('odh.giftcode.email') }}" id="report"  method="post" name="form" style="text-align:center;">
{{csrf_field()}}
<input type="email" id="email" name="email"  class="section1_input" required  pattern="[a-z0-9._%+-]+@[a-z0-9.-]+.[a-z]{2,4}$" placeholder="Đại Hiệp vui lòng để lại Email">
<span class="section1_message"style="display:block" id="result"> Vui lòng đăng ký = email của bạn để chúng tôi có thể gửi quà về</span>
<button class="btn btn-outline section1_button" id="submit_form" type ="button" name="submit">
<img class="" src="{{asset('images/ohdaihiepLandingPage/form/form_button.png')}}" style="width:100%;" alt="">
</button>
</form>

还有我的JS

$(function () {
$('#report').submit(function (e) {
e.preventDefault()
var url = e.target.action
var formData = $(this).serialize()
$.post(url, formData, function (response) {
$('.section1_input').val('');
var $res=response.message;
$('#result').text($res);
});
$(".section1_message").empty()
})
})

将按钮类型从button更改为submit。它应该工作正常,否则将事件触发器更改为click

查看此以了解更多的详细信息

最新更新