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?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.
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.
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);
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".
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');
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();
});
});
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
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
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.
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');
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();
}
});
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);
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
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;
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});
});
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"> \
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—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>
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>
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
}
});
});
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
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);
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.
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)
});
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');
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');
});
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');
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');
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