1

i have an array of checkbox (to use with php), bu i want to use ajax to make somethings with this values from checkbox, i want to get the value from each checkbox and make an ajax request.

I have this:

$("#checked").each(function(i, val){
                var k = $(i).value();
                console.log(k);
            });

but no success.

html:

<input id="checado" type="checkbox" name="ids[]" value="78">
<input id="checado" type="checkbox" name="ids[]" value="79">
<input id="checado" type="checkbox" name="ids[]" value="80">
<input id="checado" type="checkbox" name="ids[]" value="81">
4
  • this.value or $(this).val(). Commented Feb 2, 2014 at 23:26
  • 1
    there should not be multiple elements with the same id on the page, you should use a class in that case, and select as $('.checked') instead. Commented Feb 2, 2014 at 23:28
  • The HTML with the checkboxes would also be helpful if you added it to your question (though the .val() hint might be all you need to make progress) Commented Feb 2, 2014 at 23:28
  • First thing do to is read the .each documentation and figure which parameters are passed to the callback. As you can see, the first argument passed is the index of the element in the set, and the second argument is the element itself. So, why are you passing the index (a number) to jQuery? Second, if you search for .value, you'll notice that there is no such method. I highly recommend to read the jQuery tutorial before you continue. Commented Feb 2, 2014 at 23:37

2 Answers 2

1

With a small change to your HTML you can use the following JavaScript (demo):

<input class="checado" type="checkbox" name="ids[]" value="78"> 78<br/>
<input class="checado" type="checkbox" name="ids[]" value="79"> 79<br/>
<input class="checado" type="checkbox" name="ids[]" value="80"> 80<br/>
<input class="checado" type="checkbox" name="ids[]" value="81"> 81<br/>
<button id='Submit'>Submit</button>
<script>
  $('#Submit').on('click', function() {
    var values = []
    $('.checado:checked').each(function () {
        var e = $(this);
        values.push(e.val());
    });
    alert(values);
  });
</script>

For a more detailed breakdown of what is going on, the check boxes have a checked state and a value. the jQuery Selector $('.checado:checked') will return just the checked check boxes (You should use class when you have multiple elements and id only when you are identifying a single element, browsers and CSS can appear lazy about this but incorrect usage will yield unpredictable results). The other change is to grab the values by the jQuery method .val() which helps hide the input type and browser specific ways values are fetched.

Sign up to request clarification or add additional context in comments.

Comments

0

You're using jQuery, which uses it's own .val() method. Replace .value() with .val().

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.