Javascript 为什么自定义错误对话框不断消失?
我想创建一个自定义错误对话框,这是我使用的代码,但每当我运行它时,它会显示一秒钟然后消失,我在代码中找不到问题,有人能帮我吗 HTML:Javascript 为什么自定义错误对话框不断消失?,javascript,html,css,error-handling,customdialog,Javascript,Html,Css,Error Handling,Customdialog,我想创建一个自定义错误对话框,这是我使用的代码,但每当我运行它时,它会显示一秒钟然后消失,我在代码中找不到问题,有人能帮我吗 HTML: 您好@wang Hee Ra_uuGogo您的代码没有问题,除了“注册”按钮发布表单并最终重新加载页面,这就是对话框立即消失的原因,为了验证我说的内容,请尝试运行您的错误。render();通过控制台或表单外的另一个按钮进行操作,如: 单击此按钮将显示对话框,它不会消失,因此您可以尝试以不立即提交但未发现错误的方式修改代码 我希望这能回答您的问题。嗨@wan
您好@wang Hee Ra_uuGogo您的代码没有问题,除了“注册”按钮发布表单并最终重新加载页面,这就是对话框立即消失的原因,为了验证我说的内容,请尝试运行您的错误。render();通过控制台或表单外的另一个按钮进行操作,如:
单击此按钮将显示对话框,它不会消失,因此您可以尝试以不立即提交但未发现错误的方式修改代码
我希望这能回答您的问题。嗨@wang Hee Ra_uuGogo您的代码没有问题,只是“注册”按钮会发布表单并最终重新加载页面,这就是对话框立即消失的原因,为了验证我说的话,请尝试运行您的错误。render();通过控制台或表单外的另一个按钮进行操作,如:
单击此按钮将显示对话框,它不会消失,因此您可以尝试以不立即提交但未发现错误的方式修改代码
我希望这能回答你的问题。我无法重现你的错误,你能添加完整的html和JS代码吗?“ErrorDialogBox”元素是在哪里创建的?您也可以发布“注册验证”的内容function@n1kkou我让对话框在我点击注册后被调用,只是为了测试它,@tfidelis我在函数本身中创建了它,我在javascriptI的最后一行创建了一个var Error=CustomError(),我无法重现您的错误,您可以添加完整的html和JS代码吗?“ErrorDialogBox”元素是在哪里创建的?您也可以发布“注册验证”的内容function@n1kkou我让对话框在我点击注册后被调用,只是为了测试它,@tfidelis我在函数本身中创建了它,我创建了一个var Error=CustomError()在javascriptYeah的最后一行:)谢谢:)是的:)谢谢:)
<form name="signup" action="" onSubmit="return Validation_for_SignUp" method="post">
<h2 id = "FormTitle"> New Member? Sign up first! </h2>
<label>Username</label>
<input type="text" name="username" id="name"/>
<label>Email</label>
<input type="text" name="email" id="email"/>
<label>Password</label>
<input type="password" name="password" id="password"/>
<label>Confirm Password</label>
<input type="password" name="cpassword" id="cpassword"/>
<button id="Register"onclick="Error.render('Please check the field')"> Sign Up </button>
</form>
<script>
function CustomError()
{
this.render = function (dialog)
{
var WinWidth = window.innerWidth;
var WinHeight = window.innerHeight;
var ErrorDialog = document.getElementById('ErrorDialog');
var ErrorDialogBox = document.getElementById('ErrorDialogBox');
ErrorDialog.style.display = "block";
ErrorDialog.style.height = WinHeight + "px";
ErrorDialogBox.style.left = (WinWidth/2) - (550 * .05) + "px";
ErrorDialogBox.style.top = "100px";
ErrorDialogBox.style.display = "block";
document.getElementById ('DialogHead').innerHTML = "Warning!";
document.getElementById ('DialogBody').innerHTML = dialog;
ddocument.getElementById ('DialogFoot').innerHTML = '<button onclick = "Error.ok()"> OK </button>';
}
this.ok = function ()
{
document.getElementById('ErrorDialogBox').style.display = "none";
document.getElementById("ErrorDialog").style.display = "none"
}
}
var Error = new CustomError();
</script>
#ErrorDialog
{
display: none;
opacity: .8;
position: fixed;
top: 0px;
left: 0px;
background: black;
width: 100%;
z-index: 10;
}
#ErrorDialogBox
{
display: none;
position: fixed;
background: white;
border-radius: 7px;
width: 550px;
z-index: 10;
}
#DialogHead
{
background: #666;
font-size: 19px;
padding: 10px;
color: #CCC;
}
#DialogBody
{
background: #333;
padding: 20px;
color: #FFF;
}
#DialogFoot
{
background: #666;
padding: 20px;
color: #FFF;
}