我知道这是本论坛的一个常见问题,但我找不到具体的解决方案。
我要做的是根据复选框选择动态显示HTML链接。 我无法弄清楚我做错了什么,代码似乎没有做任何事情。
以下是代码。
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="description" content="GIT Courses" />
<title>GIT Courses</title>
<link href="courses3.css" rel="stylesheet" type="text/css">
<!-- We include the jQuery CSS, API, and UI from CDN in the Head -->
<!-- Most browsers have CDN fed libraries already in cache, as they are used very frequently -->
<!-- This will speed up load times, reduce bandwidth use, and serve from closest geographical servers -->
<!-- Ref: http://stackoverflow.com/questions/2180391/why-should-i-use-googles-cdn-for-jquery -->
<link
href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css"
type="text/css" rel="Stylesheet" />
<script src="http://code.jquery.com/jquery-1.9.1.js"
type="text/javascript"></script>
<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"
type="text/javascript"></script>
</head>
<body>
<div id="wrapper">
<header>
<div class=" headleft"></div>
<div class="headright">
<h1>Graphic Information Technology</h1>
</div>
<!-- end header -->
</header>
<nav>
<table class="navigation">
<thead>
<tr>
<th><a href="new2.html">home</a></th>
<th><a href="courses3.html">courses</a></th>
<th><a href="resources.html">resources</a></th>
<th><a href="labs.html">lab</a></th>
</tr>
</thead>
</table>
<!-- end nav -->
</nav>
<!-- Begin HTML elements -->
<h3>Thank you for your interest in our Developer courses!</h3>
<p>Please select which campus resources you would like to explore
more below:</p>
<!-- Here is a "Field set" Array for the Resource Check Boxes -->
<!-- We will leave the links out of the labels at this stage -->
<fieldset id="checkboxArray">
<legend>ASU Resources</legend>
<label><input type="checkbox" name="rescheck[]"value="Career Preparation" />Career Preparation</label>
<label><input type="checkbox" name="rescheck[]" value="College Advising" />College Advising</label>
<label><input type="checkbox" name="rescheck[]"value="Counseling Services" />Counseling Services</label>
<label><input type="checkbox" name="rescheck[]" value="Financial Aid" /> Financial Aid </label>
<label><input type="checkbox" name="rescheck[]"value="Fitness & Recreation" /> Fitness & Recreation </label>
<label><input type="checkbox" name="rescheck[]" value="Health Services" /> Health Services </label>
<label><input type="checkbox" name="rescheck[]"value="Housing" /> Housing </label>
<label><input type="checkbox"name="rescheck[]" value="Library" /> Library </label>
<label><input type="checkbox" name="rescheck[]" value="Parking" /> Parking </label>
<label><input type="checkbox" name="rescheck[]" value="Scholarships" /> Scholarships </label>
<label><input type="checkbox" name="rescheck[]" value="Student Employment" /> Student Employment </label>
<label><input type="checkbox" name="rescheck[]" value="Student Organizations" /> Student Organizations </label>
<label><input type="checkbox" name="rescheck[]" value="Tutoring Support" /> Tutoring Support </label>
<label><input type="checkbox" name="rescheck[]" value="Writing Support" /> Writing Support </label>
</fieldset>
<!-- Here we display the resources selected.
One way (well the only way I know how) is to Toggle visibility of DIVs based on selection.
The toggle is done within jQuery, here is the HTML side of it.
Maybe not the best practice but it works, I never did take any classes on any of this :) -->
<div class="myResources">
<p>Here are the links to your selected resources:</p>
<div class="resource" resource-label="Career Preparation">
<a href="https://students.asu.edu/careerguide/careerpreparation">Career
Preparation</a>
</div>
<div class="resource" resource-label="College Advising">
<a href="https://technology.asu.edu/advising">College Advising</a>
</div>
<div class="resource resource-label="Counseling Services">
<a href="https://students.asu.edu/counseling">Counseling
Services</a>
</div>
<div class="resource" resource-label="Financial Aid">
<a href="https://students.asu.edu/financialaid">Financial Aid</a>
</div>
<div class="resource"
resource-label="Fitness & Recreation">
<a href="http://fitness.asu.edu/">Fitness & Recreation</a>
</div>
<div class="resource" resource-label="Health Services">
<a href="https://students.asu.edu/health">Health Services</a>
</div>
<div class="resource" resource-label="Housing">
<a href="http://housing.asu.edu/home">Housing</a>
</div>
<div class="resource" resource-label="Library">
<a href="http://lib.asu.edu/">Library</a>
</div>
<div class="resource" resource-label="Parking">
<a href="https://cfo.asu.edu/pts">Parking</a>
</div>
<div class="resource" resource-label="Scholarships">
<a href="https://students.asu.edu/scholarships">Scholarships</a>
</div>
<div class="resource" resource-label="Student Employment">
<a href="https://students.asu.edu/employment">Student Employment</a>
</div>
<div class="resource" resource-label="Student Organizations">
<a href="http://asu.orgsync.com/">Student Organizations</a>
</div>
<div class="resource" resource-label="Tutoring Support">
<a href="https://studentsuccess.asu.edu/">Tutoring Support</a>
</div>
<div class="resource" resource-label="Writing Support">
<a href="https://studentsuccess.asu.edu/writingcenters">Writing
Support</a>
</div>
<!-- end of content wrapper -->
</div>
<!-- End of HTML Elements -->
<!-- Suggested best practice is to put all Scripts at the bottom of the page, just before the End of Body Tag -->
<!-- This is to speed up page loading time -->
<!-- Ref: http://stackoverflow.com/questions/2105327/should-jquery-code-go-in-header-or-footer -->
<!-- jQuery code begins here -->
<script type="text/javascript">
// We place all jQuery code inside this "Document Ready" handler to ensure everything loaded completely before running code
$(document).ready(function() {
// bind to click function
$('input[type="checkbox"]').click(function() {
// test if any are checkboxes in the array are checked
if ($('input[name="rescheck[]"]:checked').length > 0) {
// hide all resources until...
$('.resource >div').hide();
// we loop checked boxes and show div.
$("input[name='rescheck[]']:checked").each(function() {
$('.resource >div[resource-label=' + this.id + ']').show();
});
// hmm... at least that was the idea
//FIXME: Why didn't this hide?
} else {
// none checked - hide all
$('.resource >div').hide();
}
});
});
</script>
</div>
</body>
</html>
答案 0 :(得分:0)
您应该使用 Attribute Starts With Selector [name^="value"]
$('input[name^="rescheck"]:checked')
您的代码应如下所示: -
$('.resource').hide();
$('input[type="checkbox"]').click(function() {
$("input[name^='rescheck']").each(function() {
if ($(this).is(":checked"))
$('div[resource-label="'+this.value+'"]').show();
else
$('div[resource-label="'+this.value+'"]').hide();
});
});
参考 LIVE DEMO
答案 1 :(得分:0)
你的HTML中有一些错误,我不知道这些是你的实际脚本中的拼写错误还是格式化时的错误,但这行需要整理:
<div class="resource resource-label="Counseling Services">
请注意,resource-label
是当前元素类属性的一部分。
您可以尝试使用这些方法为您的jQuery做一些事情,让它更简单一些。
$('#checkboxArray').on('click', 'input[type="checkbox"]', function(event)
{
var resources = $('.myResources');
// Hide all resources
resources.children('div').hide();
// Cycle through the checkboxes siblings
$(this).siblings().each(function(index)
{
// If they are checked show the resource
if ($(this).is(':checked')
{
resources.find('div[resource-label="' + $(this).val() + '"]').show();
}
});
}
答案 2 :(得分:0)
如果复选框与相关div的顺序相同,则使用元素索引作为参考。
$('input[name^="rescheck"]').click(function() { // if checkbox clicked
var index = $(this).parent().index(); // gets parent index since nested in label
$('.resource:eq(' + index + ')').toggle() // hide or show related div
});
查看此fiddle
答案 3 :(得分:0)
这里提供的每一条回复都值得投票或少投票。谢谢,我还没有机会测试代码,但这些示例和建议非常有用。