在这种特殊情况下,我使用Google代码管理器在引导程序模式中提交表单。
我无法访问后端,这就是我使用GTM定位按钮并使用onClick的原因。
<script>
$(document).on('submit','#MyForm',function(event){
var form = $('#MyForm');
var submit = $('#ssend_btn');
form.on('submit', function(e) {
e.preventDefault();
if($('input', this).val().trim() == '' ){
//handle error message
alert("im empty and email will not send");
}
else if (submit != 'null' ){
event.preventDefault()
var formData = $(this).serialize();
console.log(formData);
$.ajax({
url: "page.php", // some php
data: formData,
datatype: "json",
type: "POST",
success: function(data) {
}
});
}
});
});
</script>
发送电子邮件后,模式不能关闭,这就是我使用ajax的原因。
如果我删除了验证,我可以发送电子邮件,但即使是空白也会提交
我有其他javascript验证,但不尊重它。
<script>
function validateForm() {
var x = document.forms["myForm"]["EMAIL"].value;
var atpos = x.indexOf("@");
var dotpos = x.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) {
error = "testingo";
document.getElementById("errorid").innerHTML = error;
return false;
}
</script>
答案 0 :(得分:1)
function validateForm() {
if ($('#EMAIL').val().trim() === '') {
return false;
}
var x = $('#EMAIL').val();
var atpos = x.indexOf("@");
var dotpos = x.lastIndexOf(".");
if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= x.length) {
error = "testingo";
document.getElementById("errorid").innerHTML = error;
return false;
}
return true;
}
$(document).ready(function() {
var form = $('#MyForm');
var submit = $('#ssend_btn');
form.on('submit', function(e) {
e.preventDefault();
if (validateForm() === false) {
//handle error message
alert("im empty and email will not send");
} else {
var formData = $(this).serialize();
console.log(formData);
$.ajax({
url: "page.php", // some php
data: formData,
datatype: "json",
type: "POST",
success: function(data) {
}
});
}
});
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="MyForm" name="MyForm">
<input type="text" name="EMAIL" id="EMAIL" placeholder="EMAIL" />
<div id="errorid"></div>
<input type="submit" id="ssend_btn" value="Submit" />
</form>
&#13;