Home >Web Front-end >JS Tutorial >How to determine whether a radio button is selected in jquery
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.
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='items']: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 = $('input[name=items][checked]').val();
4. Settings The radio button is selected
The code is as follows:
$("input[type=radio]").attr("checked",'2');//设置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!