jQuery:隐藏/显示单选框上的元素更改选中和徘徊
我已经尝试了几个小时来解决以下问题。我用一个相当复杂的 A 列表来完成此操作
- ,其中包含一个单选框选择,下面是所选单选框的描述;
- 将鼠标悬停在每个项目上时,会出现同一位置的描述(所有其他描述都会消失);
- 当鼠标未悬停在某个项目上时,将出现与选中的单选框匹配的描述(所有其他描述消失);
我完全承认我的 jQuery 知识还处于最低水平,主要是因为我自己从来不需要使用它。在谷歌上花了几个小时,并解决了一些令人费解的问题,我得到了一切,但没有找到整个问题的可行解决方案。不久前我有一个可用的 JS 脚本,代码太多了,看看我现在的情况,看起来 Jquery 可以给我一个更短、更干净的代码。
现在我
<style type="text/css">
.options .active {font-weight: bold;}
.descriptions span {display: none;}
.descriptions span.active {display: block;}
</style>
<div class="options">
<label><input name="permForumcon" type="radio" value="10" /> option1</label>
<label><input name="permForumcon" type="radio" value="50" />option2</label>
<label><input name="permForumcon" type="radio" value="70" /> option3</label>
</div>
<div class="descriptions">
<span>description1</span>
<span>description2</span>
<span>description3</span>
</div>
$(document).ready(function() {
options = $('.options > label');
descriptions = $('.descriptions > span');
options .each(function(idx) {$(this).data('slide', descriptions.eq(idx));}).hover(
function() {
options .removeClass('active');
descriptions.removeClass('active');
$(this).addClass('active');
$(this).data('slide').addClass('active');
}, function() {
switches.removeClass('active');
slides.removeClass('active');
$('input[name=permForumcon]:checked + label').addClass('active') //this is where is already doesnt seem to work
}
);});
在 Jsfiddle.net 上得到以下 HTML: http://jsfiddle.net/mqcP9/8/
剩下的问题是,它(选项中的标签和描述中的范围上添加的活动类)不会更改回所选的单选框。我尝试了一些在查看 jsfiddle 沙箱时可见的注释。谁能帮我完成最后一部分?
I've tried for a few hours to get a solution for the following problem. I did it with a rather complex
- A list with a radio-box selection, with a description of the selected radiobox below it;
- On mouse hover over each item, the description in the same position appears (all other descriptions disappear);
- When the mouse does not hover on an item, the description matching the checked radiobox will appear (all other descriptions disappear);
I fully admit that my jQuery knowledge is at a minimum level, mostly because I never need to use it myself. Spending a few hours on Google and some puzzling got me everything but a working solution for the entire problem. I had a working JS script for it a while ago with just too much code, looking at where I am now it looks like Jquery can give me a much shorter and cleaner code.
Right now I got the following HTML:
<style type="text/css">
.options .active {font-weight: bold;}
.descriptions span {display: none;}
.descriptions span.active {display: block;}
</style>
<div class="options">
<label><input name="permForumcon" type="radio" value="10" /> option1</label>
<label><input name="permForumcon" type="radio" value="50" />option2</label>
<label><input name="permForumcon" type="radio" value="70" /> option3</label>
</div>
<div class="descriptions">
<span>description1</span>
<span>description2</span>
<span>description3</span>
</div>
$(document).ready(function() {
options = $('.options > label');
descriptions = $('.descriptions > span');
options .each(function(idx) {$(this).data('slide', descriptions.eq(idx));}).hover(
function() {
options .removeClass('active');
descriptions.removeClass('active');
$(this).addClass('active');
$(this).data('slide').addClass('active');
}, function() {
switches.removeClass('active');
slides.removeClass('active');
$('input[name=permForumcon]:checked + label').addClass('active') //this is where is already doesnt seem to work
}
);});
on Jsfiddlle: http://jsfiddle.net/mqcP9/8/
The problem that remains is, it(the added active class on both the label in options and the span in descriptions) doesn't change back to the selected radiobox. I tried several things which are visible commented when looking at the jsfiddle sandbox. Anyone who could help me on the last part?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将此作为您的“关闭悬停”功能:
您选中的标签的选择器已关闭,这实际上是它的根源。
Have this as your "off-hover" function:
Your selector for the checked label was off, that was really the root of it.