0

I'm building a dynamic form which contains quite a lot of checkboxes. Because of the complexity of the form, a normal form submit isn't appropriate, and as the form is meant to be valid for multiple users at once, what I'm doing is extracting form values and storing them in JavaScript objects, before serializing them as bulk data to be sent and handled server-side.

This has worked great so far, but I've run into an issue with checkbox inputs. Despite not being checked, their value attribute is always returned from$('input[name=checkbox_name]').val(); despite whether or not the checkbox is actually checked.

I've tried to remedy this so far by:

  • Adding a value other than the default (which is on)
  • Adding a checked attribute, but I'm not entirely sure which values are valid and which aren't; for example, with Google Chrome's console I added a checked="checked" attribute/value which should check the box in real time, but it didn't
  • Placing an hidden input element with the same name attribute as the checkbox with value=0 just before the checkbox input as per http://bit.ly/MLxCht

Any answers which detail how I can extract the current value (unchecked/checked) of a checkbox would be very much appreciated.

Avicinnian
  • 1,822
  • 5
  • 37
  • 55

4 Answers4

0
$('input[name=checkbox_name]').prop('checked')

(You can also use .is(':checked') for conciseness, but it's slower.)

Ry-
  • 218,210
  • 55
  • 464
  • 476
0

Try:

$('input[name=your_name]').is(':checked');

To get actuall checked or not value

Ry-
  • 218,210
  • 55
  • 464
  • 476
kamil
  • 3,482
  • 1
  • 40
  • 64
0

try this

$(...).is(":checked")

or

$(...).attr('checked')
V1tOr
  • 369
  • 1
  • 4
  • 12
0

Instead of what others are suggesting, why not just:

$('input[name=your_name]:checked')
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592