[英]how to show div after clicking submit button for few seconds then disappear?
i have a submit button now i want to add onclick function to it to show a div tells the user the submission succeeded but for now i am getting no result i think because after submission the page reloads is there a way to turn around it?我有一个提交按钮现在我想添加 onclick function 到它以显示一个 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">
You can prevent form submit event and call your function inside it您可以阻止表单提交事件并在其中调用您的 function
// Your function to display text inside div
function myFunction() {
var x = document.getElementById("snackbar");
x.className = "show";
setTimeout(function(){ x.className = x.className.replace("show", ""); }, 3000);
}
const form = document.querySelector('#form');
// find elements of form if exists
const elements = document.querySelectorAll('#form input');
// create form data object which will has your form inputs data
const formData = new FormData();
form.addEventListener('submit', (e) => {
e.preventDefault(); // prevent event
myFunction(); // call your function to display text
// if elements exists append them to formData
if (elements) {
elements.forEach(el => {
formData.append(el.name, el.value)
})
}
// submit form
form.submit();
})
Also you can can pass some value after user submitted form and page reloaded and print html by this value using php if condition您也可以在用户提交表单和重新加载页面后传递一些值,并在条件下使用 php 按此值打印 html
<?php if ($condition): ?>
<div id="snackbar">Some text some message..</div>
<? endif; ?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.