Sprawdzanie, czy pole wyboru jest zaznaczone za pomocą jQuery

Jeśli pole wyboru jest zaznaczone, wtedy muszę tylko uzyskać wartość jako 1; w przeciwnym razie muszę ją uzyskać jako 0. Jak to zrobić za pomocą jQuery?

$("#ans").val() zawsze da mi jedno prawo w tym przypadku:

<input type="checkbox" id="ans" value="1" />
Author: TylerH, 2011-01-27

20 answers

Użyj .is(':checked'), aby określić, czy jest zaznaczone, a następnie odpowiednio ustawić wartość.

Więcej informacji tutaj.

 966
Author: Andy Mikula,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-03-17 14:38:42
$("#ans").attr('checked') 

Powie Ci, czy jest sprawdzone. Możesz również użyć drugiego parametru true/false, aby zaznaczyć / odznaczyć pole wyboru.

$("#ans").attr('checked', true);

Per comment, use prop zamiast attr when available. Np.:

$("#ans").prop('checked')
 203
Author: xaxxon,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-08-28 20:20:08

Po prostu użyj $(selector).is(':checked')

Zwraca wartość logiczną.

 85
Author: SimionBaws,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-05-09 13:13:52
// use ternary operators
$("#ans").is(':checked') ? 1 : 0;
 53
Author: Stefan Brinkmann,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-04-16 15:31:49

Odpowiedź Stefana Brinkmanna jest doskonała, ale niekompletna dla początkujących (pomija przypisanie zmiennej). Dla wyjaśnienia:

// this structure is called a ternary operator
var cbAns = ( $("#ans").is(':checked') ) ? 1 : 0;

To działa tak:

 var myVar = ( if test goes here ) ? 'ans if yes' : 'ans if no' ;

Przykład:

var myMath = ( 1 > 2 ) ? 'yes' : 'no' ;
alert( myMath );

Wpisy " nie "

Jeśli to pomoże, proszę o potwierdzenie odpowiedzi Stefana Brinkmanna.

 20
Author: crashwap,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-01-30 16:31:56

Możesz spróbować tego:

$('#studentTypeCheck').is(":checked");
 16
Author: MAnoj Sarnaik,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-07-18 14:15:54

Znalazłem ten sam problem wcześniej, mam nadzieję, że to rozwiązanie może Ci pomóc. najpierw dodaj Niestandardowy atrybut do swoich pól wyboru:

<input type="checkbox" id="ans" value="1" data-unchecked="0" />

Napisz rozszerzenie jQuery, aby uzyskać wartość:

$.fn.realVal = function(){
    var $obj = $(this);
    var val = $obj.val();
    var type = $obj.attr('type');
    if (type && type==='checkbox') {
        var un_val = $obj.attr('data-unchecked');
        if (typeof un_val==='undefined') un_val = '';
        return $obj.prop('checked') ? val : un_val;
    } else {
        return val;
    }
};

Użyj kodu, aby uzyskać wartość pola wyboru:

$('#ans').realVal();

Możesz przetestować tutaj

 15
Author: alphakevin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-07-05 08:22:56
$('input:checkbox:checked').val();        // get the value from a checked checkbox
 7
Author: nobody,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-08-28 07:42:40

Możesz również użyć:

$("#ans:checked").length == 1;
 6
Author: Fareed Alnamrouti,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-06-11 22:44:36
<input type="checkbox" id="ans" value="1" />

Jquery : var test= $("#ans").is(':checked') i zwraca prawdę lub FAŁSZ.

W funkcji:

$test =($request->get ( 'test' )== "true")? '1' : '0';
 6
Author: Rajesh RK,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-02-14 04:53:56

Użycie:

$("#ans option:selected").val()
 4
Author: Senthil Kumar Bhaskaran,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-11-03 18:52:19
function chkb(bool){
if(bool)
return 1;
return 0;
}

var statusNum=chkb($("#ans").is(':checked'));

StatusNum będzie równe 1, jeśli pole wyboru jest zaznaczone, i 0, jeśli nie jest.

EDIT: możesz również dodać DOM do funkcji.

function chkb(el){
if(el.is(':checked'))
return 1;
return 0;
}

var statusNum=chkb($("#ans"));
 4
Author: kmoney12,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-12-03 21:14:11

Istnieje kilka opcji są tam jak....

 1. $("#ans").is(':checked') 
 2. $("#ans:checked")
 3. $('input:checkbox:checked'); 

Jeśli wszystkie te opcje zwrócą wartość true, możesz ustawić wartość accourdingly.

 3
Author: Amit Maru,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-10-07 05:50:49

Przechodziłem ostatnio przez przypadek, w którym musiałem sprawdzić wartość checkbox, gdy użytkownik kliknął przycisk. Jedynym właściwym sposobem na to jest użycie atrybutu prop().

var ansValue = $("#ans").prop('checked') ? $("#ans").val() : 0;
To zadziałało w moim przypadku, może komuś się przyda.

Kiedy próbowałem .attr(':checked') zwrócił checked ale chciałem wartość boolean i .val() zwrócił wartość atrybutu value.

 2
Author: Robert,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-01-28 12:22:31

Spróbuj tego

$('input:checkbox:checked').click(function(){
    var val=(this).val(); // it will get value from checked checkbox;
})

Tutaj znacznik jest true jeśli jest zaznaczone inaczej false

var flag=$('#ans').attr('checked');

Znowu to sprawi, że cheked

$('#ans').attr('checked',true);
 1
Author: sharif2008,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-22 16:19:44

Możesz uzyskać wartość (PRAWDA / FAŁSZ) za pomocą tych dwóch metod

$("input[type='checkbox']").prop("checked");
$("input[type='checkbox']").is(":checked");
 0
Author: PPB,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-08-15 11:31:28

Jeśli chcesz mieć wartość całkowitą checked lub not try:

$("#ans: sprawdzone").długość

 0
Author: Nasaralla,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-11-14 12:40:52

Najpierw sprawdź wartość jest sprawdzana

$("#ans").find("checkbox").each(function(){
    if ($(this).prop('checked')==true){ 
    var id = $(this).val()
    }
});

Else set the 0 wartość

 0
Author: Ashok Charu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-05-07 12:25:56

$('input[id=ans]').is(':checked');

 0
Author: swathi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-07-27 11:49:47
 $("#id").prop('checked') === true ? 1 : 0;
 0
Author: Paulo Rodrigues,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-09-25 16:25:03