我试着在这个网站上四处看看,但以前的答案都没有帮助解决这个问题。我有一个包含标题和表单的 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/