3

I have this checkbox:

<input id="IsOutdoors" class="checkbox" type="checkbox" value="1">

And I'm trying to retrieve 1 if it's checked.

I've tried: var IsOutdoors = $('#IsOutdoors').val(); Which always returns 1 no matter what.

I've tried: var IsOutdoors = $("#IsOutdoors option:selected").val(); which returns nothing

I've tried: var IsOutdoors = $('#IsOutdoors').is(':checked'); which returns true/false

Any other simple ways of returning the number? Thanks!

4

5 に答える 5

4
var IsOutdoors = $('#IsOutdoors').is(':checked') ? 1 : 0;
// ||
var IsOutdoors = document.getElementById('IsOutdoors').checked ? 1 : 0;
于 2012-12-17T21:45:52.977 に答える
3
var IsOutdoors = +$('#IsOutdoors').is(':checked')
于 2012-12-17T21:47:18.710 に答える
2

チェックボックスの値を取得する場合は、次を試してください。

var IsOutdoors = $('#IsOutdoors:checked').val();
于 2012-12-17T21:47:06.037 に答える
2

How about using a ternary operator for that? What should IsOutdoors contain if the checkbox isn't checked?

var IsOutdoors = ($('#IsOutdoors').is(':checked')) ? $('#IsOutdoors').val() : false;
于 2012-12-17T21:46:15.527 に答える
1
var $checkbox = $('#IsOutdoors');

if( $checkbox.is(':checked')){
    alert($checkbox.attr('value'));
})
于 2012-12-17T21:46:52.133 に答える