将焦点绑定到单选按钮

bind focus to a radio button

本文关键字:单选按钮 绑定 焦点      更新时间:2023-09-26

我在chrome 12中工作,我的代码如下。

.HTML:

<input type="radio" class="tooltip" title="test1" name="group" value="1"> 1<br>        
<input type="radio" class="tooltip" title="test2" name="group" value="2"> 2<br>
<input type="radio" class="tooltip" title="test3" name="group" value="3" checked> 3<br>
<input type="text" class="tooltip" value="4" title="test4"><br>
<div class="result"> THIS SHOULD UPDATE WHEN YOU FOCUS ON ONE OF THE INPUTS </div>

JavaScript:

$('.tooltip').bind('focus', function(ev) {
 $('.result').html( this.value );
});

(小提琴)

是否可以将焦点事件绑定到单选按钮?你能提供替代方案吗?

如果您通过键盘设置焦点,则仅在单击按钮时才起作用,您的原始代码适用于所有浏览器。

看起来只有 IE 会在单击按钮时触发焦点事件,Chrome 只会触发单击事件。请注意,如果您单击一个然后按 TAB 它确实有效,即使在 Chrome 中也是如此。

也许

是一个错误,也许是设计使然,无论如何只需处理两者:

$(document).ready(function() {
    var _currentExecuting = false;
    $(".tooltip").bind("focus click", function() {
        if (!_currentExecuting) { 
            var oResult = $(".result");
            oResult.html(oResult.html() + "yoy oi " + this.value + "<br />"); 
            _currentExecuting = true;
            window.setTimeout(function() {
                _currentExecuting = false;
            }, 100);
        }
    });
});

由于IE浏览器在单击时会同时触发焦点和单击事件,因此我必须设置全局标志,然后在执行任何代码之前检查此标志,并在一段时间后自动清除它。

更新的小提琴。