Jak sprawdzić przycisk radiowy z jQuery?

Próbuję sprawdzić przycisk radiowy z jQuery. Oto Mój kod:

<form>
    <div id='type'>
        <input type='radio' id='radio_1' name='type' value='1' />
        <input type='radio' id='radio_2' name='type' value='2' />
        <input type='radio' id='radio_3' name='type' value='3' /> 
    </div>
</form>

Oraz JavaScript:

jQuery("#radio_1").attr('checked', true);

Nie działa:

jQuery("input[value='1']").attr('checked', true);

Nie działa:

jQuery('input:radio[name="type"]').filter('[value="1"]').attr('checked', true);

Nie działa:

Masz inny pomysł? Co przegapiłem?
Author: Community, 2011-04-14

27 answers

Dla wersji jQuery równej lub wyższej (>=) 1.6, użyj:

$("#radio_1").prop("checked", true);

Dla wersji poprzedzających (

$("#radio_1").attr('checked', 'checked');

Wskazówka: możesz także wywołać click() lub change() po naciśnięciu przycisku radiowego. Zobacz komentarze, aby uzyskać więcej informacji.

 1271
Author: Mike Thomsen,
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-12-03 04:44:42

Spróbuj tego.

W tym przykładzie kieruję go z jego nazwą wejściową i wartością

$("input[name=background][value='some value']").prop("checked",true);

Dobrze wiedzieć: w przypadku wartości wielowyrazowej będzie działać również z powodu apostrofów.

 201
Author: Vladimir Djuricic,
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-02-19 11:07:39

Jeszcze jedna funkcja prop () dodana w jQuery 1.6, która służy temu samemu celowi.

$("#radio_1").prop("checked", true); 
 93
Author: Umesh Patil,
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-01-03 08:20:57

Krótka i łatwa do odczytania opcja:

$("#radio_1").is(":checked")

Zwraca true lub false, więc można go użyć w instrukcji "if".

 73
Author: Karbaman,
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-22 06:03:15

Spróbuj tego.

Aby sprawdzić Przycisk radiowy za pomocą wartość użyj tego.

$('input[name=type][value=2]').attr('checked', true); 

Lub

$('input[name=type][value=2]').attr('checked', 'checked');

Lub

$('input[name=type][value=2]').prop('checked', 'checked');

Aby sprawdzić Przycisk radiowy za pomocą ID użyj tego.

$('#radio_1').attr('checked','checked');

Lub

$('#radio_1').prop('checked','checked');
 26
Author: Lakshmana 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
2013-06-21 06:01:39

The $.prop way is better:

$(document).ready(function () {                            
    $("#radio_1").prop('checked', true);        
});

I możesz go przetestować w następujący sposób:

$(document).ready(function () {                            
    $("#radio_1, #radio_2", "#radio_3").change(function () {
        if ($("#radio_1").is(":checked")) {
            $('#div1').show();
        }
        else if ($("#radio_2").is(":checked")) {
            $('#div2').show();
        }
        else 
            $('#div3').show();
    });        
});
 11
Author: AminSaghi,
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-07-19 08:37:08

Musisz zrobić

jQuery("#radio_1").attr('checked', 'checked');

To atrybut HTML

 8
Author: JohnP,
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-04-14 15:50:27

Spróbuj Tego:

$("input[name=type]").val(['1']);

Http://jsfiddle.net/nwo706xw/

 7
Author: user4457035,
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-15 10:08:45

Jeśli właściwość name nie działa, nie zapominaj, że id nadal istnieje. Ta odpowiedź jest dla ludzi, którzy chcą kierować {[2] } Tutaj, Jak to robisz.

$('input[id=element_id][value=element_value]').prop("checked",true);

Ponieważ własność name nie działa na mnie. Upewnij się, że nie otaczasz id i name podwójnymi / pojedynczymi cytatami.

Zdrówko!
 5
Author: Anjana Silva,
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-08-01 14:06:35

Yes, it worked for me like a way:

$("#radio_1").attr('checked', 'checked');
 5
Author: Anjan Kant,
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-10-27 07:28:57
$("input[name=inputname]:radio").click(function() {
    if($(this).attr("value")=="yes") {
        $(".inputclassname").show();
    }
    if($(this).attr("value")=="no") {
        $(".inputclassname").hide();
    }
});
 4
Author: Eray,
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-11-25 22:12:37

Powinniśmy powiedzieć, że jest to radio button.So spróbuj użyć poniższego kodu.

$("input[type='radio'][name='userRadionButtonName']").prop('checked', true);
 4
Author: UWU_SANDUN,
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-07-09 13:01:29

Na wypadek, gdyby ktoś próbował to osiągnąć podczas korzystania z jQuery UI, musisz również odświeżyć obiekt checkbox UI, aby odzwierciedlić zaktualizowaną wartość:

$("#option2").prop("checked", true); // Check id option2
$("input[name='radio_options']").button("refresh"); // Refresh button set
 4
Author: freeworlder,
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-08-15 17:05:33

Spróbuj tego

var isChecked = $("#radio_1")[0].checked;
 3
Author: user2190046,
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-03-20 09:12:06

Get value:

$("[name='type'][checked]").attr("value");

Wartość zestawu:

$(this).attr({"checked":true}).prop({"checked":true});

Przycisk radiowy kliknij Dodaj attr zaznaczone:

$("[name='type']").click(function(){
  $("[name='type']").removeAttr("checked");
  $(this).attr({"checked":true}).prop({"checked":true});
});
 3
Author: Naim Serin,
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-23 20:19:11

Mam jakiś powiązany przykład do ulepszenia, co powiesz na to, czy chcę dodać nowy warunek, powiedzmy, jeśli chcę, aby schemat kolorów był ukryty po kliknięciu na wartość statusu projektu, z wyjątkiem kostki brukowej i płyt chodnikowych.

Przykład jest tutaj:

$(function () {
    $('#CostAnalysis input[type=radio]').click(function () {
        var value = $(this).val();

        if (value == "Supply & Lay") {
            $('#ul-suplay').empty();
            $('#ul-suplay').append('<fieldset data-role="controlgroup"> \

Http://jsfiddle.net/m7hg2p94/4/

 2
Author: azim hamdan,
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-10-27 09:46:16

Używam tego kodu:

Przepraszam za Angielski.

var $j = jQuery.noConflict();

$j(function() {
    // add handler
    $j('#radio-1, #radio-2').click(function(){

        // find all checked and cancel checked
        $j('input:radio:checked').prop('checked', false);

        // this radio add cheked
        $j(this).prop('checked', true);
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<fieldset class="section">
  <legend>Radio buttons</legend>
  <label>
    <input type="radio" id="radio-1" checked>
    Option one is this and that&mdash;be sure to include why it's great
  </label>
  <br>
  <label>
    <input type="radio" id="radio-2">
    Option two can be something else
  </label>
</fieldset>
 2
Author: volitilov,
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-03-20 17:39:17

Spróbuj z przykładem

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="myForm">
<input type="radio" name="radio" value="first"/> 1 <br/>
<input type="radio" name="radio" value="second"/> 2 <br/>
</form>


<script>
$(document).ready(function () {
    $('#myForm').on('click', function () {
        var value = $("[name=radio]:checked").val();

        alert(value);
    })
});
</script>
 2
Author: saroj,
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-06-21 07:31:16

Spróbuj tego

$(document).ready(function(){
    $("input[name='type']:radio").change(function(){
        if($(this).val() == '1')
        {
          // do something
        }
        else if($(this).val() == '2')
        {
          // do something
        }
        else if($(this).val() == '3')
        {
          // do something
        }
    });
});
 2
Author: Jordan Jelinek,
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-11-01 09:52:26

Spróbuj tego

 $("input:checked", "#radioButton").val()

If checked zwraca True if not checked zwraca False

jQuery v1.10.1
 1
Author: pst,
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-04-24 11:45:27

Czasami powyższe rozwiązania nie działają, możesz spróbować poniżej:

jQuery.uniform.update(jQuery("#yourElementID").attr('checked',true));
jQuery.uniform.update(jQuery("#yourElementID").attr('checked',false));

Innym sposobem, który możesz wypróbować, jest:

jQuery("input:radio[name=yourElementName]:nth(0)").attr('checked',true);
 1
Author: Pankaj Bhagwat,
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-09-17 03:03:06

Mam podobny problem, prostym rozwiązaniem jest po prostu użyć:

.click()

Każde inne rozwiązanie zadziała, jeśli odświeżysz radio po wywołaniu funkcji.

 1
Author: user3148673,
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-10-05 01:55:10
function rbcitiSelction(e) {
     debugger
    $('#trpersonalemail').hide();
    $('#trcitiemail').show();
}

function rbpersSelction(e) {
    var personalEmail = $(e).val();
    $('#trpersonalemail').show();
    $('#trcitiemail').hide();
}

$(function() {  
    $("#citiEmail").prop("checked", true)
});
 1
Author: MadhaviLatha Bathini,
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-02-02 17:43:40
$("#radio_1").attr('checked', true);
//or
$("#radio_1").attr('checked', 'checked');
 0
Author: dominicbri7,
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-04-14 15:50:32

Spróbuj Tego:

$(document).ready(function(){
  $("#Id").prop("checked", true).checkboxradio('refresh');
});
 0
Author: user3721473,
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-02 06:05:47

attr akceptuje dwa ciągi znaków.

Prawidłowy sposób to:

jQuery("#radio_1").attr('checked', 'true');
 0
Author: Iuri Matos,
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-25 20:14:12

Attr akceptuje dwa ciągi znaków.

Prawidłowy sposób to:

jQuery("#radio_1").attr('checked', 'true');
 0
Author: Koko Monsef,
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-20 18:23:11