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 - JS代码不起作用

javascript - ngView 中指令使用的远程数据存储在哪里? (AngularJS)

javascript - Select2 错误 - 无响应

javascript - ChartJs 鼠标悬停错误(显示以前的图表)

javascript - 函数不读取对象

javascript - 谷歌地图,样式不适用于带类的标记 img,但适用于 img[src]

javascript - React : this. setState 未重置状态

php - html 加载到页面后可以设置 cookie 值吗?

javascript - 显示 :none; in select multiple doesn't work on IE 8