gpt4 book ai didi

javascript:表单不会仅在 IE 中提交,但是它适用于所有其他浏览器

转载 作者:行者123 更新时间:2023-12-03 11:17:31 25 4
gpt4 key购买 nike

在这段代码上,它工作得很好,它检查所有 if 条件,并且在除 IE8 和 IE 之外的所有浏览器中都可以。当所有登录值都正确输入后,在提交表单时,它可以在除 IE、IE8 之外的所有浏览器中运行,为什么它在 IE8 中不起作用?有什么线索吗?

当所有输入都正确时,将显示以下内容。这在 IE8 中不起作用。有什么帮助吗?

<div id="FormSubmit" class="FormSubmit">
<p>Form submitted successfully</p>
</div>

不知 Prop 体问题出在哪里。它适用于其他浏览器。

<!DOCTYPE html>
<html>
<head>
<title>Bootstrap Example</title>

<!--[if IE]>
<link rel="stylesheet" href="IEStyles.css" type="text/css" media="screen" />
<![endif]-->
</head>

<body>
<div class="container">

<div id="FormSubmit" class="FormSubmit">
<p>Form submitted successfully</p>
</div>
<div class="LogIn">
<form name="loginForm" method="post" onsubmit="javascript:return validateForm(this);">
<input type="text" name="username" placeholder="Firstname Lastname"/>
<span class="user-name">Name should not be empty</span>
<input type="password" name="password" placeholder="Password"/>
<input type="password" name="password1" placeholder="Confirm password"/>
<span class="password">Password does not be match</span>
<input type="email" name="email" placeholder="Email"/>
<span class="email">Email is not valid</span>
<input type="url" name="url" placeholder="Website"/>
<span class="urlcontent">Invalid Website URL</span>
<input name="submit" type="submit" value="Submit"/>
</form></div> </div>

<script type="text/javascript">
var browserName;
window.onload = function(){
document.getElementsByName("username")[0].value="";
document.getElementsByName("password")[0].value="";
document.getElementsByName("password1")[0].value="";
document.getElementsByName("email")[0].value="";
document.getElementsByName("url")[0].value="";
document.getElementById("FormSubmit").style.display="none";
document.querySelector('span[class="user-name"]').style.display="none";
document.querySelector('span[class="password"]').style.display="none";
document.querySelector('span[class="urlcontent"]').style.display="none";
document.querySelector('span[class="email"]').style.display="none";
document.getElementsByName("username")[0].focus();
browserName = navigator.appName;
//alert(browserName);
}

function validateEmail(email) {
var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(email);
}

function validateURL(curUrl){
alert("Hey, ULLLL");
var re = /^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/;
return re.test(curUrl);
}

function validateForm(form) {
var returnValue=true;
if(loginForm.username.value.length < 6){
returnValue = false;
document.querySelector('span[class="user-name"]').style.display="";
loginForm.username.value = "";
loginForm.username.focus();
return returnValue;

}
if (loginForm.password.value.length < 6){
returnValue = false;
alert("Your password must be at least\n6 characters long.\n Please try again.");
loginForm.password.value = "";
loginForm.password1.value = "";
loginForm.password.focus();
return returnValue;
}
if (loginForm.password.value != loginForm.password1.value){
returnValue = false;
document.querySelector('span[class="password"]').style.display="";
loginForm.password.value = "";
loginForm.password1.value = "";
loginForm.password1.focus();
return returnValue;
}
if (loginForm.url.value.length == 0){
returnValue = false;
document.querySelector('span[class="urlcontent"]').style.display="";
loginForm.url.focus();
return returnValue;
}

if ((loginForm.email.value.length != 0) && (browserName == "Microsoft Internet Explorer")){
returnValue = false;
var imEmail = validateEmail(loginForm.email.value);
returnValue = imEmail;
if (returnValue == false) document.querySelector('span[class="email"]').style.display="";

loginForm.email.focus();
return returnValue;

}

if ((loginForm.url.value.length != 0) && (browserName == "Microsoft Internet Explorer")){
//alert("Yessssssss!");
returnValue = false;
var imURL = validateURL(loginForm.url.value);
returnValue = imURL;
if (returnValue == false) document.querySelector('span[class="urlcontent"]').style.display="";
loginForm.url.focus();//alert(returnValue);
return returnValue;
}

if(returnValue!=false) {

document.getElementById("FormSubmit").style.display="";

return false;
}
}

</script>



</body>

</html>

最佳答案

在 IE 中返回在这里:

if ((loginForm.email.value.length != 0) && (browserName == "Microsoft Internet Explorer")){
returnValue = false;
var imEmail = validateEmail(loginForm.email.value);
returnValue = imEmail;
if (returnValue == false) document.querySelector('span[class="email"]').style.display="";

loginForm.email.focus();
return returnValue;
^^^^^^^^^^^^^^^^^^^
}

其中if语句实际上是这样的:

if (returnValue == false) {document.querySelector('span[class="email"]').style.display="";}
^ ^
loginForm.email.focus();
return returnValue;

我猜你想要的是当它是错误的时候返回。您确实应该用大括号将 if 语句括起来,否则只有第一个语句位于检查之下

if (returnValue == false) {
document.querySelector('span[class="email"]').style.display="";
loginForm.email.focus();
return returnValue;
}

与“loginForm.url”相同

关于javascript:表单不会仅在 IE 中提交,但是它适用于所有其他浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27271373/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com