1<!-- Find values of checked boxes with jquery -->
2<div class="fruit">
3 <label>
4 <span>Apple</span>
5 <input onchange="checkValues()" name="apple" type="checkbox" value="apple">
6 </label>
7
8 <label>
9 <span>Banana</span>
10 <input onchange="checkValues()" name="banana" type="checkbox" value="banana">
11 </label>
12
13 <label>
14 <span>Grape</span>
15 <input onchange="checkValues()" name="grape" type="checkbox" value="grape">
16 </label>
17</div>
18
19<script>
20 function checkValues() {
21 $('.fruit input:checkbox').each(function(index, element) {
22 if (element.checked === true) {
23 console.log(element.value)
24 }
25 });
26 }
27</script>
1function onSubmit()
2{
3 var fields = $("input[name='list']").serializeArray();
4 if (fields.length === 0)
5 {
6 alert('nothing selected');
7 // cancel submit
8 return false;
9 }
10 else
11 {
12 alert(fields.length + " items selected");
13 }
14}
15
16// register event on form, not submit button
17$('#subscribeForm').submit(onSubmit)
18
19//**************************************************
20// HTML
21<form method="POST" id="subscribeForm">
22 <fieldset id="cbgroup">
23 <div><input name="list" id="list0" type="checkbox" value="newsletter0" >zero</div>
24 <div><input name="list" id="list1" type="checkbox" value="newsletter1" >one</div>
25 <div><input name="list" id="list2" type="checkbox" value="newsletter2" >two</div>
26 </fieldset>
27 <input name="submit" type="submit" value="submit">
28</form>
29