【发布时间】:2016-03-22 08:34:00
【问题描述】:
是否可以通过鼠标悬停事件来控制单选按钮的显示?
目标是让它在鼠标悬停时看起来是选中的,而在鼠标离开时看起来是未选中的。但它只有在被点击时才会被检查,就像评分一样。
【问题讨论】:
-
你尝试过什么吗?如果是这样,请分享,以便我们为您提供帮助...
是否可以通过鼠标悬停事件来控制单选按钮的显示?
目标是让它在鼠标悬停时看起来是选中的,而在鼠标离开时看起来是未选中的。但它只有在被点击时才会被检查,就像评分一样。
【问题讨论】:
你可以这样使用:
$("input:radio").hover(function(){
$(this).prop('checked', true);
}, function(){
//MouseOut event
$(this).prop('checked', false);
}).click(function(){
//Unbind the hover event for all the radio buttons
$("input:radio").unbind('mouseenter mouseleave')
//Check clicked radio button
$(this).prop('checked', true);
});
我为你做了一个小提琴:http://jsfiddle.net/B6qBz/
【讨论】:
在稍微摆弄之后,我想出了这个......看看这个fiddle 看看是否有诀窍......哦,既然你没有提到任何特定的语言,所以我用 jQuery 实现了这个...希望对您有所帮助
这可能不是完美的解决方案,但绝对符合要求:
HTML:
<input type="radio" name="cols" id="col-1" />
<br />
<input type="radio" name="cols" id="col-2" />
<br />
<input type="radio" name="cols" id="col-3" />
JS:
var clicked = false;
var clickedId;
$('input').click(function () {
clicked = true;
clickedId = $(this).attr('id');
});
$('input').hover(function () {
$(this).prop('checked', true);
}, function () {
if (clicked === true) {
$('#' + clickedId).prop('checked', true);
} else {
$(this).prop('checked', false);
}
});
【讨论】: