我在ajax中编写了一个代码,用于将html中表单选项卡中的表单值转换为另一个php文件email.php
但它没有在alert()中显示消息。
<form method = "post" id = "my_form">
<h1> Thank you for visiting our website! Please take some time to fill out our service questions! </h1>
<h2> The ratings given are F, D, C, B and A. </h3>
<h3> How would you rate the service that this webpage provides?? </h3>
<input type = "text" font-size = "15px" id = "name">
<h3> How would you rate the organization and usability of this webpage?? </h3>
<input type = "text" font-size = "15px" id = "name">
<h3> How would you rate the professionalism of this webpage?? </h3>
<input type = "text" font-size = "15px" id = "name">
<h3> How would you rate the overall look of this webpage??</h3>
<input type = "text" font-size = "15px" id = "name">
<br /><br />
<input type = "submit" id = "submit" value = "submit" name = "submit">
</form>
<script type = "text/javascript">
var my_form = document.getElementById("my_form");
my_form.addEventListener("submit", function(event) {
var name = document.getElementById("name").value;
var tomatch = /[a-f]/i;
if (!tomatch.test(name)) {
event.preventDefault();
window.alert("The name can only contain letters from a-f");
}
}. false);
</script>
我已经编写了这段代码但是当我提交发送按钮时,它没有显示提交的消息表单。
答案 0 :(得分:1)
您错误地使用了Javascript onclick功能。
<script>
$(function () {
$('#send_message').on('click', function (e) {
e.preventDefault();
$.ajax({
type: 'post',
url: 'email.php',
data: $('#contact_form1').serialize(),
success: function (res) {
alert('form was submitted'+res);
}
});
});
});
</script>
对于提交按钮,只需提供。
答案 1 :(得分:0)
Sou应添加click
或submit
个事件:
$('#contact_form1').on('click', function (e) {
答案 2 :(得分:0)
尝试使用以下代码
$(document).on('submit', '#contact_form1', function (e) {
e.preventDefault();
$.ajax({
type: 'post',
url: 'email.php',
data: $('#contact_form1').serialize(),
success: function () {
alert('hii form was submitted');
}
});
});
希望此代码能够提供帮助