javascript - jQuery .hide() 方法不隐藏 div

标签 javascript jquery html

我试着在这个网站上四处看看,但以前的答案都没有帮助解决这个问题。我有一个包含标题和表单的 div。

基本上我希望在提交表单后隐藏 div 中的所有内容,所以我一直试图隐藏 div。但不幸的是,我所做的一切都没有得到这项工作。这是我的代码:

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
  $(document).ready(function(){
    $("#shirtForm").submit(function(e){
      $("#question").hide();
    });
  });
</script>
</head>
<body>
  <div id="question">
      <h3>What colour shirt was Eamon wearing today?</h3>
      <form id="shirtForm" onsubmit="shirtValidator()">
        <input type="radio" name="shirtColour" id="redShirt"> Red <br>
        <input type="radio" name="shirtColour" id="blueShirt"> Blue <br>
        <input type="radio" name="shirtColour" id="yellowShirt"> Yellow <br>
        <input type="radio" name="shirtColour" id="noShirt"> He isn't wearing a shirt <br>
        <input type="submit" value="Enter" onclick="shirtValidator()">
      </form>
  </div>

</body>
</html>

在此先感谢您的任何帮助。

最佳答案

问题不在于您的代码,它正在运行,但事情发生得太快以至于您没有注意到它们。

hide() 函数正在运行,问题是您的 DOM 可能在提交表单后重新加载并且 hide() 发生得如此之快以至于效果只能瞬间可见。

试试这个:

<script>
$(document).ready(function(){
    $("#shirtForm").submit(function(e){
      e.preventDefault()
      $("#question").hide();
      return false;
    });
  });
</script>

它对我有用。

我发现了一个使用 hide here 的好例子.

关于javascript - jQuery .hide() 方法不隐藏 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44289338/

相关文章:

javascript - 为什么 Javascript 默认不支持继承?

javascript - 在javascript中点击两次鼠标绘制连接器

javascript - 使用浏览器调整大小调整、移动和缩放图像和文本 div

HTML &lt;script type=template> 官方?

javascript - 如何访问其他对象兄弟的值?

javascript - 函数内函数的参数

jquery - jqGrid 工具栏按列搜索运算符

javascript - 默认情况下不显示引导 Accordion 中第一个的文本

Html CSS 没有将元素放在同一行

xslt - 使用 XSLT,如何将每个标签变成一个 div,其类与标签名称相匹配?