Jennifer Anthony
Jennifer Anthony

Reputation: 2277

Check if checkbox is checked

Why does the following code not work?

Example: http://jsfiddle.net/ZZe5X/18/

$('.submit').submit(function () {
    e.preventDefault()
    if ($('input[type="checkbox"]').is(':checked')) {
        alert('ok')
    } else {
        alert('no')
    }
})

Upvotes: 1

Views: 652

Answers (5)

Doozer Blake
Doozer Blake

Reputation: 7797

  1. You didn't have a submit class
  2. You had .submit() set on a button, it needs to be .click
  3. You weren't iterating through each checked box, which is what I assume you wanted to be doing.

See the tweaked example: http://jsfiddle.net/ZZe5X/33/

Upvotes: 0

voigtan
voigtan

Reputation: 9031

Okey:

  1. you are missing the event: $('.submit').submit(function () { should be $('.submit').submit(function (e) {
  2. The button is missing a type="submit" and it should be inside the form element.
  3. The Form element is missing the class submit

this should work: http://jsfiddle.net/voigtan/ZZe5X/26/

Upvotes: 1

deviousdodo
deviousdodo

Reputation: 9172

See this: http://jsfiddle.net/ZZe5X/16/

Upvotes: 0

Vikas Naranje
Vikas Naranje

Reputation: 2412

Please try this.

   $('.submit').submit(function () {

        if ($('input[type="checkbox"]').is(':checked')) {
            alert('ok');
        } else {
            alert('no');
        }
      return false;
    })

Upvotes: 0

Paul
Paul

Reputation: 141927

You don't have a submit button in a form with the class submit, so your submit event handler never executes. Add class="submit" to your form tag and change <button>Click me</button> to <input type="submit" value="Click me" /> and move it to before your </form> tag.

Upvotes: 0

Related Questions