Dowiedz się, czy przycisk opcji jest zaznaczony w JQuery?

Mogę ustawić przycisk radiowy na checked fine, ale chcę ustawić rodzaj "słuchacza", który aktywuje się po sprawdzeniu określonego przycisku radiowego.

Weźmy na przykład następujący kod:

$("#element").click(function()
{ 
    $('#radio_button').attr("checked", "checked");
});

Dodaje atrybut sprawdzony i wszystko jest dobrze, ale jak bym to zrobił dodawanie alertu. Na przykład, gdy przycisk radiowy jest zaznaczony bez pomocy funkcji click?

Author: Ashish Bahl, 2010-02-16

15 answers

$('#element').click(function() {
   if($('#radio_button').is(':checked')) { alert("it's checked"); }
});
 928
Author: David Hedlund,
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-02 11:52:01

Jeśli masz grupę przycisków radiowych o tym samym atrybutie name i podczas wysyłania lub jakiegoś zdarzenia chcesz sprawdzić, czy jeden z tych przycisków został zaznaczony, możesz to zrobić po prostu za pomocą następującego kodu:

$(document).ready(function(){
  $('#submit_button').click(function() {
    if (!$("input[name='name']:checked").val()) {
       alert('Nothing is checked!');
        return false;
    }
    else {
      alert('One of the radio buttons is checked!');
    }
  });
});

Źródło

JQuery API Ref

 132
Author: Parag,
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-08-30 14:50:39

Jako, że rozwiązanie Paraga rzuciło dla mnie błąd, oto moje rozwiązanie (łączące Davida Hedlunda i Paraga):

if (!$("input[name='name']").is(':checked')) {
   alert('Nothing is checked!');
}
else {
   alert('One of the radio buttons is checked!');
}

To działało dobrze dla mnie!

 37
Author: Patrick DaVader,
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-04-10 14:32:37

Musiałbyś powiązać Zdarzenie kliknięcia pola wyboru, ponieważ zdarzenie zmiany nie działa w IE.

$('#radio_button').click(function(){
    // if ($(this).is(':checked')) alert('is checked'); 
    alert('check-checky-check was changed');
});

Teraz, gdy programowo zmienisz stan, musisz wywołać również to zdarzenie:

$('#radio_button').attr("checked", "checked");
$('#radio_button').click();
 30
Author: Znarkus,
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
2010-02-16 11:57:29

/ / sprawdzenie przez klasę

if($("input:radio[class='className']").is(":checked")) {
     //write your code         
}

/ / Sprawdź przez nazwę

if($("input:radio[name='Name']").is(":checked")) {
         //write your code         
}

/ / Sprawdź przez DANE

if($("input:radio[data-name='value']").is(":checked")) {
         //write your code         
}
 17
Author: Nanhe Kumar,
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-01-06 11:51:14

Innym sposobem jest użycie prop (jQuery >= 1.6):

$("input[type=radio]").click(function () {
    if($(this).prop("checked")) { alert("checked!"); }
});
 9
Author: Saeb Amini,
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-09-19 13:25:56

Jeśli nie chcesz funkcji kliknięcia użyj funkcji zmiany Jquery

$('#radio_button :checked').live('change',function(){
alert('Something is checked.');
});

To powinna być odpowiedź, której szukasz. jeśli używasz Jquery w wersji powyżej 1.9.1 spróbuj użyć, ponieważ funkcja live została przestarzała.

 6
Author: iCezz,
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-05-02 07:49:13

Rozwiązanie będzie proste, ponieważ po zaznaczeniu określonego przycisku radiowego wystarczy "słuchacz". Zrób to: -

if($('#yourRadioButtonId').is(':checked')){ 
// Do your listener's stuff here. 
}
 6
Author: Shah-Kodes,
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-05-24 09:10:15

... Dzięki chłopaki... wszystko, czego potrzebowałem, to 'wartość' sprawdzonego przycisku radiowego, gdzie każdy przycisk radiowy w zestawie miał inny identyfikator...

 var user_cat = $("input[name='user_cat']:checked").val();
Mi pasuje...
 5
Author: Bill Warren,
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-09-14 12:08:53

Praca ze wszystkimi typami przycisków radiowych i przeglądarek

if($('#radio_button_id')[0].checked) {
   alert("radiobutton checked")
}
else{
   alert("not checked");
}

Praca Jsfiddle Tutaj

 4
Author: Thyagu,
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-06-01 07:38:42

Dynamicznie generowany przycisk radiowy Sprawdź radio get value

$("input:radio[name=radiobuttonname:checked").val();

Przy zmianie dynamicznego przycisku radiowego

$('input[name^="radioname"]').change(function () {if (this.value == 2) { }else{}});
 3
Author: Dhaval Kariya,
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-05-18 08:36:33

Spróbuj tego

    if($('input[name="radiobutton"]:checked').length == 0) {
        alert("Radio buttons are not checked");
    }
 2
Author: Muhammad Aamir Ali,
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-12-30 10:37:52

Spróbuj tego:

alert($('#radiobutton')[0].checked)
 1
Author: Nandha kumar,
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-01 20:29:42

$( '. radio-button-class-name'). is ('checked') nie zadziałało u mnie, ale następny kod zadziałał dobrze:

    if(typeof $('.radio-button-class-name:checked').val() !== 'undefined'){
     // radio button is checked
    }
 1
Author: Jokerius,
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-03 14:19:06
$("#radio_1").prop("checked", true);

Dla wersji jQuery przed 1.6, użyj:

$("#radio_1").attr('checked', 'checked');
 -1
Author: Jayendra Manek,
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-27 05:27:28