Seasons123 / blog-FE

web前端相关issue is my blog :lollipop:
2 stars 0 forks source link

使用jquery获取单选按钮radio的选中值 #97

Closed Seasons123 closed 6 years ago

Seasons123 commented 6 years ago

一、简单来说: var val = $('input[name="sex"]:checked').val();

二、整体代码:

<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="utf-8">
    <title>获取单选按钮选中的值</title>
</head>
<body>
<label><input type="radio" name="sex" value="m" checked="checked">男</label>
<label><input type="radio" name="sex" value="f">女</label>
<button>提交</button>
<script type="text/javascript" src="jquery-1.11.3.min.js"></script>
<script type="text/javascript">
    $(function(){
        $('button').click(function(){
            var val = $('input[name="sex"]:checked').val();
            console.log(val);   
        });
    })
</script>
</html>