开发者

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 {
 开发者_如何学JAVA       alert('no')
    }
})


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/


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


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.


Please try this.

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

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


  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/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜