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
Release: 2023-01-04 09:37:45
Original
11407 people have browsed it

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>
Copy after login

二, jquery gets the value of the radio radio button

The code is as follows:

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

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());
}
}
Copy after login

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();
Copy after login

4. Settings The radio button is selected

The code is as follows:

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template