0

Here is the script (javascript & jQuery) :

$('#button_submit').click(function() {
    var check = { 'opt[]' : []};
    $("input:checked").each(function() {
        check['opt[]'].push($(this).val());
    });
}

and the html one :

<html>
<body>
    <h4>Data Options</h4>
    <form id="myForm">
        <input type='checkbox' name='opt[]' value='1'id='ck1' /> Counts<br>
        <input type='checkbox' name='opt[]' value='2'id='ck2' /> Male<br>
        <input type='checkbox' name='opt[]' value='3'id='ck3' /> Female<br>
        <input type="button" id="button_submit" value="go">
    </form>
</body>
</html>

Im not getting the checked checkboxes, is anything wrong?

3 Answers 3

2

try to add $(document).ready:

<script type='text/javascript'>
$(document).ready(function(){
  $('#button_submit').click(function() 
  {...}
})
</script>
Sign up to request clarification or add additional context in comments.

Comments

0

Given the markup above this is how I would populate check

$(document).ready(function() {
    $('#button_submit').click(function() {
       var check = { 'opt[]' : []};
       $('input[name="opt[]"]:checked').each(function() {
           check['opt[]'].push($(this).val());
       });

       // just for a bit of debugging
       for(var x in check['opt[]']) {
           $('span').append(check['opt[]'][x]);
       }
       // assuming you want to do something other than just let it submit

       return false;      
    });
});

See http://jsfiddle.net/8Dm9C/1/ for an example

Comments

0

If you just want to check multiple checkboxes

$("input:checkbox").prop("checked", true )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.