jQuery: Is checkbox checked?
Blogs20112011-10-10
jQuery: Is checkbox checked?
for checkbox, radio, or select menu, sometimes we need to judge which one(s) is/are selected. Here I use checkbox as example, list 2 very common ways to check if they are checked or not?
- Use filtering is() and :checked Selector
- Use HTML checkbox attribute ’checked‘.
// 1. use filtering is() and :checked Selector.
if($('#checkboxs_id').is(':checked')){...}
// 2. use checkbox attribute 'checked'.
if ($('#checkBox').attr('checked')) {...}The following is the checkbox interaction between jQuery and PHP codes:
// 1. use jQuery to get the checkbox group values.
$('input[name="mycheckboxes"]:checked')
.map(function(){ return $(this).val(); })
.get().join(",");
// 2. Then explode in PHP:
$mycheckboxes = explode(',',$_GET['mycheckboxes']);