Home >Web Front-end >JS Tutorial >How to determine whether a radio button is selected in jquery

How to determine whether a radio button is selected in jquery

coldplay.xixi
coldplay.xixiOriginal
2020-12-23 11:36:1911390browse

Jquery method to determine whether a radio button is selected: 1. Use the checked attribute to determine whether it is selected. The code is [if ($(this).attr("checked")) {alert("checked"); }]; 2. jquery gets the value of the radio button.

How to determine whether a radio button is selected in jquery

The operating environment of this tutorial: windows7 system, jquery3.2.1 version, Dell G3 computer.

Recommended: jquery video tutorial

Jquery method to determine whether the radio button is selected:

1. Use the checked attribute to judge the selection

radio cannot be judged by "checked" equality, only true is used to judge

The code is as follows:

<script type="text/javascript">
        $(function () {
            $("input").click(function () {
                if ($(this).attr("checked")) {
                    alert("选中了");
                }
            });
        });
    </script>
</head>
<body>
<input type="radio"/>
</body>
</html>

二, jquery gets the value of the radio radio button

The code is as follows:

$("input[name=&#39;items&#39;]:checked").val();

Another: Determine whether the radio is selected and obtain the selected value

The code is as follows :

function checkradio(){
var item = $(":radio:checked");
var len=item.length;
if(len>0){
  alert("yes--选中的值为:"+$(":radio:checked").val());
}
}

3. Get the value of a group of radio selected items

The code is as follows:

var item = $(&#39;input[name=items][checked]&#39;).val();

4. Settings The radio button is selected

The code is as follows:

$("input[type=radio]").attr("checked",&#39;2&#39;);//设置value=2的项目为当前选中项

Related free learning recommendations:js video tutorial

The above is the detailed content of How to determine whether a radio button is selected in jquery. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn