【问题标题】:how to show div after clicking submit button for few seconds then disappear?单击提交按钮几秒钟后如何显示div然后消失?
【发布时间】: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' ])) {} 有什么想法吗?
  • 如果您使用带有 jQ​​uery 的 XHR 请求,您可以尝试 event.preventDefault() 提交 form 事件。
  • @Harun Yilmaz 但这会阻止我被提交
  • 如果您使用 XHR 请求,您不希望表单提交并重定向到另一个 URL。请看这个帖子:stackoverflow.com/questions/20352799/…

标签: javascript html jquery submit


【解决方案1】:

您可以阻止表单提交事件并在其中调用您的函数

// 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();
    
})

您还可以在用户提交表单和页面重新加载后传递一些值,并在条件下使用 php 通过该值打印 html

<?php if ($condition): ?>
    <div id="snackbar">Some text some message..</div>
<? endif; ?>

【讨论】:

  • 我有 if(isset($_POST['submit'])) {在这里我做我的东西将东西插入我的数据库和任何东西} 我现在应该把这些东西放在你的代码中吗? @Vlad Brezitskiy
  • 虽然此代码可以解决问题,including an explanation 说明如何以及为什么解决问题将真正有助于提高您的帖子质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提出问题的人。请edit您的答案添加解释并说明适用的限制和假设。 From Review
  • @Vlad Brezitskiy 但如果我阻止提交我的表单,我将如何将我的信息获取到我的数据库中
  • 将提交表单,在上面的代码中,我查看表单元素并使用此数据触发提交
猜你喜欢
  • 2022-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-30
  • 2021-11-04
  • 1970-01-01
  • 2012-06-11
  • 2011-12-30
相关资源
最近更新 更多