gpt4 book ai didi

jquery - ToggleClass 基于复选框状态

转载 作者:行者123 更新时间:2023-11-28 19:01:18 30 4
gpt4 key购买 nike

所以我得到了一些导航,其中包含 float 在传统 anchor 旁边的表单复选框。如果未选中相应的复选框,我想添加一个类(基本上将设置样式以使 anchor 标记变暗)。系统地执行此操作的最佳方法是什么?

<div id="navPod">
<form method="post" action="#" id="contentFilterForm" name="contentFilterForm">
<input type="hidden" name="q" value="<?php echo $q;?>" />
<input class="checkbox" type="checkbox" <?php if($pagetype == 'home'){ echo "checked=\"checked\"";} if($pagetype == 'projects'){ echo "checked=\"checked\"";} if($proj){ echo "checked=\"checked\"";} ?> name="pr" value="1" id="filterProjects" />
<input class="checkbox" <?php if($pagetype == 'home'){ echo "checked=\"checked\"";} if($pagetype == 'playground'){ echo "checked=\"checked\"";} if($playground){ echo "checked=\"checked\"";} ?> type="checkbox" name="pl" value="1" id="filterPlayground" />
<input class="checkbox" type="checkbox" <?php if($pagetype == 'home'){ echo "checked=\"checked\"";} if($pagetype == 'about'){ echo "checked=\"checked\"";} if($culture){ echo "checked=\"checked\"";} ?> name="cu" value="1" id="filterCulture" />
<input class="checkbox" type="checkbox" <?php if($pagetype == 'home'){ echo "checked=\"checked\"";} if($pagetype == 'leadership'){ echo "checked=\"checked\"";} if($leadership){ echo "checked=\"checked\"";} ?> name="le" value="1" id="filterLeadership" />
<input class="checkbox" type="checkbox" <?php if($pagetype == 'home'){ echo "checked=\"checked\"";} if($pagetype == 'careers'){ echo "checked=\"checked\"";} if($jobs){ echo "checked=\"checked\"";} ?> name="ca" value="1" id="filterCareers" />

<input class="checkbox" <?php if($pagetype == 'home'){ echo "checked=\"checked\"";} if($pagetype == 'blog'){ echo "checked=\"checked\"";} if($blog){ echo "checked=\"checked\"";} ?> type="checkbox" name="bl" value="1" id="filterBlog" />
<input class="checkbox" <?php if($pagetype == 'home'){ echo "checked=\"checked\"";} if($pagetype == 'delicious'){ echo "checked=\"checked\"";} if($links){ echo "checked=\"checked\"";} ?> type="checkbox" name="li" value="1" id="filterDelicious" />

<input id="refreshSubmit" type="button" value="" onclick="createURLS()"/>
</form>

<div class="subnav">
<a id="projectsLink" title="Projects" href="/projects/" <?php if($pagetype == 'projects'){ echo "class=\"currentPage\""; } ?> >Projects</a><br />
<a id="playgroundLink" title="Playground" href="/playground/" <?php if($pagetype == 'playground'){ echo "class=\"currentPage\""; } ?> >Playground</a><br />
<a id="aboutLink" title="About Centerline Digital" href="/about/" <?php if($pagetype == 'about'){ echo "class=\"currentPage\""; } ?> >About Us</a><br />
<a id="leadershipLink" title="Leadership" href="/leadership/" <?php if($pagetype == 'leadership'){ echo "class=\"currentPage\""; } ?> >Leadership</a><br />
<a id="careersLink" title="Careers" href="/careers/" <?php if($pagetype == 'careers'){ echo "class=\"currentPage\""; } ?> >Careers</a><br />
<a id="blogLink" title="Blog" href="/blog/" <?php if($pagetype == 'blog'){ echo "class=\"currentPage\""; } ?> >Blog</a><br />
<a id="thoughtsLink" title="Thoughts and Links" href="/thoughts/" <?php if($pagetype == 'delicious'){ echo "class=\"currentPage\""; } ?> >Thoughts and Links</a><br />

<a target="_blank" title="Subscribe to Custom Feed" id="feedLink" href="/dyn/">Subscribe to Custom Feed</a>
</div>
</div>

最佳答案

基于 html 我会试试这个

$('input:checkbox').each(function(){
var anchor_id = $(this).attr('id').replace('filter','').toLowerCase();;
$('#'+anchor_id+'Link').toggleClass('someclass', $(this).attr('checked'));
});
$('input:checkbox').change(function(){
var anchor_id = $(this).attr('id').replace('filter','').toLowerCase();;
$('#'+anchor_id+'Link').toggleClass('someclass', $(this).attr('checked'));
})

working demo

关于jquery - ToggleClass 基于复选框状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5638245/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com