简体   繁体   中英

checkbox change event not working

i'm Trying to include an onclick event for a checkbox in my laravel application

This is my Input Section

<input type="checkbox" id="early_access" name="early_access">

And the function

$(document).ready(function() {
  $('#early_access').change(function() {
    alert("booyah");
  });
});

The function is not working ..

Guys i found the problem and solved it Unfortunately the frontend designer had some other function running for changing the style of checkbox and it was inherited from the master layout ...

$(document).ready(function(){
$('input').iCheck({
    checkboxClass: 'icheckbox_flat-blue',
    radioClass: 'iradio_flat-blue'
});
});

this was running instead of my script :( .. when i removed it everything worked

Have you checked if your jquery was loaded ?

window.onload = function() {
    if (window.jQuery) {  
        // jQuery is loaded  
        alert("Yeah!");
    } else {
        // jQuery is not loaded
        alert("Doesn't Work");
    }
}

Use on

$(document).ready(function() {
    $('#early_access').on("change", function() {
        alert("booyah");
    });
 });

If you can, I suggest that you use your selector #early_access as an argument.

$(document).ready(function() {
     $(document).on("change", "#early_access" function() {
         alert("booyah");
     });
});

It might not be relevant with an id but if you use a class based attribute and you dynamically change the DOM, it will still be responsive. But that might be beyond the point of your question.

Your code is absolutely fine see this fiddle https://jsfiddle.net/avinash8526/nL1crxn0/1/

 $(document).ready(function() {
  $('#early_access').change(function() {
    alert("I will only work with check uncheck");
  });
});

Further you have onchange event applied, hence this alert will only work if you check/uncheck this box

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM