Javascript 你能在Internet Explorer上捕获这些单选按钮事件吗?

Javascript 你能在Internet Explorer上捕获这些单选按钮事件吗?,javascript,jquery,html,internet-explorer,Javascript,Jquery,Html,Internet Explorer,考虑以下示例代码: <div class="containter" id="ControlGroupDiv"> <input onbeforeupdate="alert('bingo 0'); return false;" onclick="alert('click 0');return false;" id="Radio1" type="radio" value="0" name="test" checked="checked" /> <input o

考虑以下示例代码:

<div class="containter" id="ControlGroupDiv">
  <input onbeforeupdate="alert('bingo 0'); return false;" onclick="alert('click 0');return false;" id="Radio1" type="radio" value="0" name="test" checked="checked" />  
  <input onbeforeupdate="alert('bingo 1'); return false;" onclick="alert('click 1');return false;"  id="Radio2" type="radio" value="1" name="test" />   
  <input onbeforeupdate="alert('bingo 2'); return false;" onclick="alert('click 2');return false;"  id="Radio3" type="radio" value="2" name="test" />   
  <input onbeforeupdate="alert('bingo 3'); return false;" onclick="alert('click 3');return false;"  id="Radio4" type="radio" value="3" name="test" />   
  <input onbeforeupdate="alert('bingo 4'); return false;" onclick="alert('click 4');return false;"  id="Radio5" type="radio" value="4" name="test" />   
  <input onbeforeupdate="alert('bingo 5'); return false;" onclick="alert('click 5');return false;"  id="Radio6" type="radio" value="5" name="test" />   
</div>
警报框弹出,您将其关闭,单击事件永远不会触发。所以我们认为我们已经弄明白了,但不,不可能那么容易。如果删除警报并将其更改为:

onmousedown="return false;"
它不起作用。另一个单选按钮清除,您单击的按钮上的click事件将触发<代码>onbeforeupdate仍然不会触发

我们认为这可能是时间问题(这一直是一个理论,尽管它很少是真的),所以我尝试了以下方法:

onmousedown="for (i=0; i<100000; i++) {;}; return false;"

onmousedown=“for(i=0;i是的,这是一个奇怪的错误。我确实设法想出了一个解决办法。我在这里使用了一点原型来处理类名。它在IE和FF中工作。您可能可以使用选择器而不是原始循环来缩短它

<form name="f1">
<input type=radio onmouseover="recordMe()" onclick="clickCheck();return false" checked value="A" name="r1" id="radio1" />
<input type=radio onmouseover="recordMe()" onclick="clickCheck();return false" value="B" name="r1" id="radio2" />
</form>
<script type="text/javascript">
function recordMe() {
    for(var x=0;x<document.f1.r1.length;x++) {
        if(document.f1.r1[x].checked) {
            $(document.f1.r1[x].id).addClassName('radioChecked')
        }
    }
}
function clickCheck() {
    for(var x=0;x<document.f1.r1.length;x++) {
        if($(document.f1.r1[x].id).hasClassName('radioChecked')) {
            $(document.f1.r1[x].id).checked=true
        }
    }
}
</script>

函数recordMe(){

对于(var x=0;x您的目标是什么?如果您只想捕获何时进行了更改,请在onclick事件上运行您的逻辑。(这将通过onchange绕过IE的bug)


如果您试图更改单选按钮的预期行为-我强烈建议您不要这样做。

这些方法都不起作用-我们最终使用复选框jQuery插件,用可移动图像替换复选框和单选按钮。这意味着我们可以控制禁用控件的视图


PITA,但它可以工作。

我也有这个问题。我们目前实现了jquery,所以我用它来重新检查相应的一个。显然,您不需要jquery来设置复选框的属性

function something(type){//type can be 1 or 0
     if(confirm("You will lose all unsaved changes. Continue?")){
     //Do stuff
          switch(type){
               case 1:
                break;
               case 0;
                break;
                default:
                //nothing;
                 break;
          }//!End of switch 
      }else{//They clicked canel
      //Reset checkboxes in IE
          if(type==1){$('#IDofCheckbox1').attr('checked', true);}
          else{$('#IDofCheckbox2').attr('checked', true);}
      }//!End of if they clicked cancel

不必将
onclick
属性设置为
return false
,您可以将未选择的单选按钮设置为
disabled
。这会给您相同的效果,只是未选择的单选按钮会灰显。

我们唯一要做的是,根据客户的请求,不灰显禁用的单选按钮和复选框。这是一个快速而简单的解决方案,适用于所有浏览器上的所有复选框和除IE(当然)之外的所有东西上的所有单选按钮。呵呵,我喜欢客户请求!-我希望我不允许单击的东西与我可以单击的东西完全一样。-没有意义!;-)呃,在这种情况下,我不能争辩太多。灰显的复选框很难阅读,Firefox/IE以不同的方式呈现相关标签(包括IE将灰显文本转换为凿刻的“3D”的令人讨厌的方式)“这看起来像是对白色以外的任何东西的废话。正如另一个帖子中的其他人所说,替换“是/否”真/假文本在视觉上不如复选框或单选按钮那么容易识别,表单的其余部分显然是只读的,没有灰显,那么为什么支票/单选控件应该有所不同呢?”?
function something(type){//type can be 1 or 0
     if(confirm("You will lose all unsaved changes. Continue?")){
     //Do stuff
          switch(type){
               case 1:
                break;
               case 0;
                break;
                default:
                //nothing;
                 break;
          }//!End of switch 
      }else{//They clicked canel
      //Reset checkboxes in IE
          if(type==1){$('#IDofCheckbox1').attr('checked', true);}
          else{$('#IDofCheckbox2').attr('checked', true);}
      }//!End of if they clicked cancel