, ,

jquery: checking checkbox

Tuesday, November 25 Leave a Comment

dalam javascript, kerja-kerja checking ni memang rumit. tapi dengan menggunakan jquery, kerja-kerja tersebut dipermudahkan.

kat sini aku nak share kod yang aku sering gunakan untuk buat checking checkbox bagi memastikan user dah tick checkbox sebelum dia klik submit.

contoh: confirm dulu sebelum proceed.

semua kerja-kerja checking tersebut di buat dalam hanya beberapa baris kod jquery seperti dibawah (tak sampai 30 baris pun) :

<script type="text/javascript">
$(document).ready(function() {
//this is Jquery language.

//this function is to make sure user select query to be deleted.
$("#removeDraftQuery").click(function (){
    var _isSelected = false;

    //jquery loop each of checkboxes
    $("input[type=checkbox]").each(function(){
        var isChecked = $(this).is(":checked");
        if(isChecked) _isSelected = true;
    });

    if(!_isSelected) {
        alert('Please select query to delete.'); return false;
    } else {
        if(!confirm('Are you sure to remove selected Query?')) return false;
    }
}); // end click function
}); //end document ready       
</script>

p/s: tu yang aku suka guna jquery. library dia kecik. simple.

0 comments »