返回false,不取消在Chrome中单击单选按钮,当没有选择其他值

Return false doesn't cancel radio buttons click in Chrome when no other value selected

本文关键字:单选按钮 其他 单击 有选择 false 取消 Chrome 返回      更新时间:2023-09-26

我正在尝试取消单选按钮点击。我有一个像这样的jquery点击处理程序:

$("#myList").on("click","input", function(){
    return false;
});

当已经有一个选定的值时,这可以正常工作。但是,如果我从一个没有选定值的组中单击一个单选按钮,则返回false不起作用,并且我单击的元素实际上被选中了。

什么线索吗?

编辑:

下面是演示问题的小提琴:

http://jsfiddle.net/SFZMm/2/

Edit2:

刚刚发现这只发生在chrome上:(Firefox和IE工作如预期。也许有个变通办法?

这里,试试这个JS_FIDDLE-DEMO

它检测父容器上的点击。而不是单选按钮本身。

 //listen to click on parent div,span,li for example. 
$("#myList-parent").click(function(){
       alert('You clicked radio!');
        //Now put your logic here. I decide based on the radio button value. 
       if($('input:radio[name=type]:checked').val() == "UnSelectable"){
          alert($('input:radio[name=type]:checked').val());
              return false;    
       }
   });

更新(Chrome): JS-FIDDLE-DEMO似乎Chrome有on click的问题,所以用on mousedown替换它(或使用两者?):

 $("input").on("mousedown",function(e) {
        e.preventDefault();
        if ($(this).is(':checked')) {
            alert('This radio button was checked in mousedown');                
        } else {
            alert('This radio button was not checked in mousedown');
        }                    
    });

使用change方法代替click。

$("#myList").on("change","input", function(e){
    if(something){
        e.preventDefault();
    } else{
  //for test
   alert('not true');
  }
});