1

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')
    }
})
1
  • I don't see in your example any .submit class and a submit input. check this jsfiddle.net/ZZe5X/32 Commented Oct 5, 2011 at 11:30

5 Answers 5

1

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/

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

Comments

0

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

Comments

0

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.

Comments

0

Please try this.

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

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

Comments

0
  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/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.