Javascript 如果innerHTML值等于指定值,则停止提交
我正在设计一个网页,检查指定的电子邮件是否可用。如果它是可用的,那么我必须停止提交表格 我使用ajax实时检查电子邮件,并将响应更新为span消息。如果单击“提交”按钮,即使电子邮件已在数据库中可用,表单也会被提交并重定向到另一个页面。请帮我摆脱这一切。提前谢谢Javascript 如果innerHTML值等于指定值,则停止提交,javascript,jquery,ajax,Javascript,Jquery,Ajax,我正在设计一个网页,检查指定的电子邮件是否可用。如果它是可用的,那么我必须停止提交表格 我使用ajax实时检查电子邮件,并将响应更新为span消息。如果单击“提交”按钮,即使电子邮件已在数据库中可用,表单也会被提交并重定向到另一个页面。请帮我摆脱这一切。提前谢谢 <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"><
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
function checkemail() {
var email=document.getElementById( "UserEmail" ).value;
if(email) {
$.ajax({
type: 'post',
url: 'check.php',
data: {
user_email:email,
},
success: function (response) {
$( '#email_status' ).html(response);
}
});
}
}
function validateForm(){
var a=document.getElementById("email_status").innerHTML;
var b="Email Already Exist";
if(a==b)
alert('Yes');
else
alert('No');
}
</script>
</head>
<body>
<form method="POST" action="insertdata.php" onsubmit="return validateForm();">
<input type="text" name="useremail" id="UserEmail" onkeyup="checkemail();">
<span id="email_status"></span><br>
<input type="submit" name="submit_form" value="Submit">
</form>
函数checkemail(){
var email=document.getElementById(“UserEmail”).value;
如果(电子邮件){
$.ajax({
键入:“post”,
url:'check.php',
数据:{
用户_电子邮件:电子邮件,
},
成功:功能(响应){
$('#email_status').html(回复);
}
});
}
}
函数validateForm(){
var a=document.getElementById(“email_status”).innerHTML;
var b=“电子邮件已存在”;
如果(a==b)
警惕(“是”);
其他的
警告(“否”);
}
当我发送一封已在db中的电子邮件时的预期结果:
对
实际结果:
没有您的代码存在设计问题。 1.最大的一个问题是,您在用户键入时发出ajax调用请求,这可能会导致您的开销很大。 2.相同的设计导致验证无法正常工作 请允许我提出一个建议
<form method="POST" action="insertdata.php" id="form" onsubmit="return false;">
<input type="text" name="useremail" id="UserEmail" >
<span id="email_status"></span><br>
<input type="submit" name="submit_form" value="Submit" onClick="checkemail();">
</form>
在这种设计方法中,代码不起作用
1.点击按钮执行检查电子邮件功能
2.checkemail函数检查电子邮件数组是否包含来自文本输入的电子邮件,如果电子邮件在数组中,则在控制台中写入日志,如果不包含,则执行ajax请求。
3.如果电子邮件在数据库中,则会制作其他日志,否则会提交表格
这种方法提供了保存用户可能写的每封电子邮件的能力。我还建议您使用ajax脚本,而不是返回文本消息来返回代码(可能是0或1),这样比较更安全
最后但并非最不重要的一点是,尽管我不知道您打算在哪里使用该代码,但请记住,bot可能会绕过此java脚本代码,直接命中您的服务器端脚本。因此,您还应该考虑服务器端检查。
如果您需要更多帮助或澄清,请毫不犹豫地询问。在验证函数中,您不会返回true或false,这是我在这里找到的最好的解决方案。多谢!我说对了!
var emails = [];
function checkemail() {
var email = document.getElementById('UserEmail').value;
if (email) {
if (!emails.includes(email)) {
$.ajax({
type: 'post',
url: 'check.php',
data: {
user_email: email,
},
success: function (response) {
$('#email_status').html(response);
if (response == 'Email Already Exist') {
console.log("email "+email+" is a spam")
emails.push(email);
} else {
document.getElementById('form').setAttribute('onsubmit', 'return true;');
document.getElementById('form').submit;
}
}
});
}else{
console.log("email "+email+" is a spam")
}
}
}