显示/隐藏 div 不起作用
我在从下拉菜单中选择时显示我的 div 时遇到问题,我收到 $ is not Defined
我已经调整了我的代码,使其不具有重复的 id。当我加载页面时,它显示所有 3 个 div,但一旦我从下拉列表中选择一个选项,它们就会全部隐藏。
JS:
$(document).ready(function(){
$('#requiredOption').change(function(){
$('#newwebsiteDiv,#websiteredevelopmentDiv,#otherDiv').hide();
$(this).find('option:selected').attr('id') + ('Div').show();
});
});
HTML:
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<title>Template 2011</title>
<link rel="stylesheet" href="_assets/css/style.css">
</head>
<body>
<header>
<div id="logo">Template Here</div>
<nav>
<ul>
<li><a href="/">Home</a></li>
<li><a href="#">About Me</a></li>
<li><a href="quote.html">Free Quote</a></li>
<li><a href="#">Showcase</a></li>
<li><a href="#">Contact Me</a></li>
</ul>
</nav>
</header>
<section id="content">
<h1>Free Quote</h1>
<p>Please fill out the below questionnaire to receive your free web development quote</p>
<form action="" method="post" accept-charset="utf-8">
<select name="requiredOption" id="requiredOption">
<option id="pleaseselect" value="pleaseselect">Please Select Your Required Quote</option>
<option id="newwebsite" value="newwebsite">New Website</option>
<option id="websiteredevelopment" value="websiteredevelopment">Website Redevelopment</option>
<option id="other" value="other">Other</option>
</select>
<p><input type="submit" value="submit"></p>
</form>
<section id="newwebsiteDiv">
<p>New Website</p>
</section>
<section id="websiteredevelopmentDiv">
<p>Website Redevelopment</p>
</section>
<section id="otherDiv">
<p>Other</p>
</section>
</section>
<section id="sidebar">
<div id="box_one">
<p>Box One</p>
</div>
<div id="box_two">
<p>Box Two</p>
</div>
<div id="box_three">
<p>Box Three</p>
</div>
</section>
<footer>
<p>This is the footer</p>
</footer>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>
<script src="_assets/js/js.js" type="text/javascript"></script>
</body>
I am having an issue getting my div's to show on select from the drop down menu, I am getting $ is not defined
I have adjusted my code to not have duplicate ids. When I load the page it shows all 3 divs but as soon as I select an option from the drop down they all go and hide.
JS:
$(document).ready(function(){
$('#requiredOption').change(function(){
$('#newwebsiteDiv,#websiteredevelopmentDiv,#otherDiv').hide();
$(this).find('option:selected').attr('id') + ('Div').show();
});
});
HTML:
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<title>Template 2011</title>
<link rel="stylesheet" href="_assets/css/style.css">
</head>
<body>
<header>
<div id="logo">Template Here</div>
<nav>
<ul>
<li><a href="/">Home</a></li>
<li><a href="#">About Me</a></li>
<li><a href="quote.html">Free Quote</a></li>
<li><a href="#">Showcase</a></li>
<li><a href="#">Contact Me</a></li>
</ul>
</nav>
</header>
<section id="content">
<h1>Free Quote</h1>
<p>Please fill out the below questionnaire to receive your free web development quote</p>
<form action="" method="post" accept-charset="utf-8">
<select name="requiredOption" id="requiredOption">
<option id="pleaseselect" value="pleaseselect">Please Select Your Required Quote</option>
<option id="newwebsite" value="newwebsite">New Website</option>
<option id="websiteredevelopment" value="websiteredevelopment">Website Redevelopment</option>
<option id="other" value="other">Other</option>
</select>
<p><input type="submit" value="submit"></p>
</form>
<section id="newwebsiteDiv">
<p>New Website</p>
</section>
<section id="websiteredevelopmentDiv">
<p>Website Redevelopment</p>
</section>
<section id="otherDiv">
<p>Other</p>
</section>
</section>
<section id="sidebar">
<div id="box_one">
<p>Box One</p>
</div>
<div id="box_two">
<p>Box Two</p>
</div>
<div id="box_three">
<p>Box Three</p>
</div>
</section>
<footer>
<p>This is the footer</p>
</footer>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>
<script src="_assets/js/js.js" type="text/javascript"></script>
</body>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将 jQuery 包含在
中。
还:
应该是:
Put the jQuery include in the
<head>
.also:
should be:
jquery和js文件导入就可以了;
首先,您的代码不包含任何包含 id“select”的元素。您的隐藏代码应该类似于“evan”
the jquery and js file import thing is ok;
first,your code not include any element contains id "select".your hide code should like 'evan'
我会做这样的事情:
I would do something like this: