首页 > 请问 jQuery 怎么看单选框有没有被选中?

请问 jQuery 怎么看单选框有没有被选中?

选出来,一直是 on 值,请怎么用jQuery 读取 单选框的值?

<div>
        <input type="radio" name='one' id="danxuankuangshiyan1" />
        <input type="radio" name='one' id="danxuankuangshiyan2" />

        <button id='s1'>单选框1的值</button>
        <button id='s2'>单选框2的值</button>
        <br>
        <span></span>
</div>
        $("#s1").on("click",radioselect1);
        $("#s2").on("click",radioselect2);

        function radioselect1()
        {
            data = $(":radio[id=danxuankuangshiyan1]").val();
            $("span").text(data);

        }


        function radioselect2()
        {
            data = $(":radio[id=danxuankuangshiyan2]").val();
            $("span").text(data);

        }

$("input:checked")

很简单,附上地址jquery选择器


是否被选中 检测该元素是否存在checked属性即可


$(xxx).prop('checked')


你要分清楚你究竟是要获取 单选框的值 还是 单选框是否选中,这是两个概念。

<input type="radio" name="one" id="r1" value="1">
<input type="radio" name="one" id="r2" value="2">
$("#s1").on("click",radioselect1);
$("#s2").on("click",radioselect2);

function radioselect1()
{
    console.log($('#r1').val())  // 如果具体到某个单选框,那么无论是否选中值是不变的
    console.log($('[name=one]:checked').val())  //使用 :checked 获取的是这一组中选中的那个值
    console.log($('#r1').prop('checked')) // 通过 checked 获取是否选中

}


function radioselect2()
{
    console.log($('#r2').val())  
    console.log($('[name=one]:checked').val())  
    console.log($('#r2').prop('checked')) 

}
【热门文章】
【热门文章】