【发布时间】:2021-04-02 18:39:48
【问题描述】:
我现在有一个提交按钮,我想向它添加 onclick 功能以显示一个 div 告诉用户提交成功但现在我没有得到任何结果,我认为因为提交后页面重新加载是否有办法转身吗?
function myFunction() {
var x = document.getElementById("snackbar");
x.className = "show";
setTimeout(function() {
x.className = x.className.replace("show", "");
}, 3000);
}
#snackbar {
visibility: hidden;
min-width: 250px;
margin-left: -125px;
background-color: #333;
color: #fff;
text-align: center;
border-radius: 2px;
padding: 16px;
position: fixed;
z-index: 1;
left: 50%;
bottom: 30px;
font-size: 17px;
}
#snackbar.show {
visibility: visible;
-webkit-animation: fadein 0.5s, fadeout 0.5s 2.5s;
animation: fadein 0.5s, fadeout 0.5s 2.5s;
}
@-webkit-keyframes fadein {
from {
bottom: 0;
opacity: 0;
}
to {
bottom: 30px;
opacity: 1;
}
}
@keyframes fadein {
from {
bottom: 0;
opacity: 0;
}
to {
bottom: 30px;
opacity: 1;
}
}
@-webkit-keyframes fadeout {
from {
bottom: 30px;
opacity: 1;
}
to {
bottom: 0;
opacity: 0;
}
}
@keyframes fadeout {
from {
bottom: 30px;
opacity: 1;
}
to {
bottom: 0;
opacity: 0;
}
}
<div id="snackbar">Some text some message..</div>
<input type="submit" name="submit " onclick="return myFunction();" value="confirm">
【问题讨论】:
-
您的代码似乎运行良好。有几个错别字。请看一下我刚刚编辑的sn-p。
-
@Harun Yilmaz 感谢您的回答,但是当我单击提交时我的页面正在重新加载并且我没有得到 div 按摩,我在 if(isset($_POST['submit' ])) {} 有什么想法吗?
-
如果您使用带有 jQuery 的 XHR 请求,您可以尝试
event.preventDefault()提交form事件。 -
@Harun Yilmaz 但这会阻止我被提交
-
如果您使用 XHR 请求,您不希望表单提交并重定向到另一个 URL。请看这个帖子:stackoverflow.com/questions/20352799/…
标签: javascript html jquery submit