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:
- you are missing the event:
$('.submit').submit(function () {
should be$('.submit').submit(function (e) {
- The button is missing a type="submit" and it should be inside the form element.
- 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;
})
- You didn't have a submit class
- You had .submit() set on a button, it needs to be .click
- 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/
精彩评论