Returning false does not cancel keystrokes in Chrome when no other value is selected

I am trying to undo a click of a switch. I have a jquery click handler:

$("#myList").on("click","input", function(){

    return false;

});

This works great when the value is already selected. But if I click on a radio button from a group that does not have a selected value, false is returned, and the element I clicked is actually selected.

Any clues?

Edit:

Here is a fiddle demonstrating the problem:

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

Edit2:

It just turned out that this only happens on chrome :( Firefox and IE work as expected. Perhaps a workaround?

+4
source share
2 answers

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');
        }                    
    });
+2

.

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

Source: https://habr.com/ru/post/1533670/


All Articles