如何用jQuery检查一个单选按钮?

我试图用jQuery来检查一个单选按钮。以下是我的代码。

<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>

和JavaScript。

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

不起作用。

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

不起作用。

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

不起作用。

你有别的想法吗?我错过了什么?

对该问题的评论 (1)
解决办法

对于jQuery的版本等于或高于(>=)1.6,使用。

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

对于(

评论(15)

试试这个。

在这个例子中,我以它的输入名称和值为目标。

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

很高兴知道。 在多字值的情况下,它也可以工作,因为省略号。

评论(8)

还有一个jQuery 1.6中增加的函数prop(),作用相同。

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

简洁易读的选项。

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

它返回true或false,所以你可以在"if&quot.语句中使用它。 语句中使用。

评论(1)

试试这个。

使用来检查单选按钮,使用这个。

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

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

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

使用ID来检查单选按钮,请使用以下方法。

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

$('#radio_1').prop('checked','checked');
评论(0)

试试这个。

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

http://jsfiddle.net/nwo706xw/

评论(1)

$.prop的方式更好。

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

你可以像下面这样测试。

$(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();
    });        
});
评论(1)

你必须要做的是

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

这就是HTML属性

评论(0)

是的,它对我来说就像一个方法。

$("#radio_1").attr('checked', 'checked');
评论(0)
$("input[name=inputname]:radio").click(function() {
    if($(this).attr("value")=="yes") {
        $(".inputclassname").show();
    }
    if($(this).attr("value")=="no") {
        $(".inputclassname").hide();
    }
});
评论(0)

如果属性name不起作用,别忘了id还存在。 这个答案是为那些想把id作为目标的人准备的,你可以这样做。

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

因为属性 "name "对我不起作用。 请确保你不要用双引号/单引号包围idname

干杯!

评论(0)

我们要告诉它是一个 "广播 "按钮,所以请用下面的代码试试。

$("input[type='radio'][name='userRadionButtonName']").prop('checked', true);
评论(0)

如果有人想在使用jQuery UI时实现这个功能,你还需要刷新UI复选框对象以反映更新后的值。

$("#option2").prop("checked", true); // Check id option2
$("input[name='radio_options']").button("refresh"); // Refresh button set
评论(0)

试试这个

$(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
        }
    });
});
评论(0)

试试这个例子

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="radio" name="radio" value="first"/> 1 <br/>
<input type="radio" name="radio" value="second"/> 2 <br/>


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

        alert(value);
    })
});
</script>
评论(0)

获取价值。

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

设定值: {{{8904945}}

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

单选按钮点击添加attr选中。

$("[name='type']").click(function(){
  $("[name='type']").removeAttr("checked");
  $(this).attr({"checked":true}).prop({"checked":true});
});
评论(0)

我用这个代码:

我'对不起英语。

<!--开始片段。 js hide: false console: true babel.false --> -- begin snippet: js hide: false console: true false -->

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>

  Radio buttons

    <input type="radio" id="radio-1" checked>
    Option one is this and that—be sure to include why it's great

  <br>

    <input type="radio" id="radio-2">
    Option two can be something else

<!--结束片段-->

评论(0)

试试这个

var isChecked = $("#radio_1")[0].checked;
评论(0)
function rbcitiSelction(e) {
     debugger
    $('#trpersonalemail').hide();
    $('#trcitiemail').show();
}

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

$(function() {  
    $("#citiEmail").prop("checked", true)
});
评论(0)

我有一些相关的例子需要加强,如果我想添加一个新的条件,比方说,如果我想在点击项目状态值后隐藏颜色方案,除了铺路和铺路板。

例子在这里。

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

        if (value == "Supply & Lay") {
            $('#ul-suplay').empty();
            $('#ul-suplay').append(' \

http://jsfiddle.net/m7hg2p94/4/

评论(0)