jquery获取单选框的值

    <!DOCTYPE html>  
    <html>  
        <head>  
            <meta charset="utf-8" />  
            <script type="text/javascript" src="js/jquery-3.3.1.min.js" ></script>  
            <title></title>  
        </head>  
        <body>  
            <input type="radio" name="type" value="0"/>类型0  
            <input type="radio" name="type" value="1"/>类型1  
            <input type="radio" name="type" value="2"/>类型2  
            <input type="radio" name="type" value="3"/>类型3  
            <br />  
            <input type="button" value="获取单选框的值" onclick="getRadio()"/>  
            <input type="button" value="为单选框赋值" onclick="setRadio()"/>  
            <br />  
            <input type="checkbox" name="status" value="0"/>状态0  
            <input type="checkbox" name="status" value="1"/>状态1  
            <input type="checkbox" name="status" value="2"/>状态2  
            <input type="checkbox" name="status" value="3"/>状态3  
            <br />  
            <input type="button" value="获取多选框的值" onclick="getCheckbox()"/>  
            <input type="button" value="为复选框赋值" onclick="setCheckbox()" />  
        </body>  
        <script>  
            function getRadio(){  
                var radioValue = $("input[name='type']:checked").val();  
                console.log(radioValue);  
            }  
            function setRadio(){  
                var i = 2;
                $("input[name='type']").prop("checked",false);
                $("input[name='type'][value='"+i+"']").prop("checked",true);
                
            }  
            function getCheckbox(){  
                var checkboxValue = [];  
                $("input[name='status']:checked").each(function(){  
                    checkboxValue.push($(this).val());  
                });  
                console.log(checkboxValue);  
            }  
            function setCheckbox(){  
                var checkboxArray = [1,3];  
                $("input[name='status']").prop("checked",false); 
                $.each(checkboxArray,function(index,value){  
                    $("input[name='status'][value='"+value+"']").prop("checked",true);  
                });  
            }  
        </script>  
    </html>  


猜你喜欢

转载自blog.csdn.net/shuair/article/details/80740768