我有4个无线电输入。jQuery热键只能使用一次
我给他们分配了热键。 (热键是1,2,3和4)
这里是我的HTML代码:
<label>
<input type="radio" value="2" id="1" name="1">[c2]
</label>
<label>
<input type="radio" value="4" id="2" name="1">[c4]
</label>
<label>
<input type="radio" value="1" id="3" name="1">[c1]
</label>
<label>
<input type="radio" value="3" id="4" name="1">[c3]
</label>
和的JavaScript代码:
$(document).keypress(function(e) {
if(e.charCode == 49) {
console.log("1");
$('input:radio[id=1]').attr('checked',true);
}
if(e.charCode == 50) {
console.log("2");
$('input:radio[id=2]').attr('checked',true);
}
if(e.charCode == 51) {
console.log("3");
$('input:radio[id=3]').attr('checked',true);
}
if(e.charCode == 52) {
console.log("4");
$('input:radio[id=4]').attr('checked',true);
}
});
你也可以看到,我将console.log
事件写入脚本以便观察。
对我而言,奇怪的是,我可以通过热键每个单选按钮选择一次redio按钮。
*
如果我按1,它选择了正确的复选框,并给了我正确的控制台日志。
然后我按2,并工作相同。
但是,如果我再次按1,它会给我控制台日志,但热键不起作用。
这里是的jsfiddle: https://jsfiddle.net/f07evno0/
我怎样才能让热键工作不止一次在一个会议?
另外看看这个:http://stackoverflow.com/questions/5874652/prop-vs-attr有解释。 –
你应该扩大这个答案,并解释为什么这个作品与OP的方法相比:) –