Home > Web Front-end > JS Tutorial > body text

How does jquery determine whether the radio is selected?

藏色散人
Release: 2023-01-04 09:37:37
Original
3771 people have browsed it

The jquery method to determine whether the radio is selected: first open the corresponding code file; then use the "$('input:radio:checked').val();" method to obtain the value in the radio; finally pass the judgment Whether the radio is empty can be used to determine whether the radio is selected.

How does jquery determine whether the radio is selected?

The operating environment of this tutorial: windows7 system, jquery1.10.0 version. This method is suitable for all brands of computers.

Recommended: "jquery video tutorial"

JQuery determines whether the radio is selected and gets the selected value:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JQuery radio</title>
<script type="text/javascript" language="javascript" 
src="JavaScript/jquery-1.6.1.min.js" ></script>
<script type="text/javascript" language="javascript">
 /*------判断radio是否有选中,获取选中的值--------*/
    $(function(){
         $("#btnSubmit").click(function(){
            var val=$(&#39;input:radio[name="sex"]:checked&#39;).val();
            if(val==null){
                alert("什么也没选中!");
                return false;
            }
            else{
                alert(val);
            }
            var list= $(&#39;input:radio[name="list"]:checked&#39;).val();
            if(list==null){
                alert("请选中一个!");
                return false;
            }
            else{
                alert(list);
            }           
         });
     });
</script>
</head>
 
<body>
<form id="form1" >
<input type="radio"  name="sex" value="男" />男
<input type="radio" name="sex" value="女" />女
<br />
<input type="radio"  name="list" value="十分满意" />十分满意
<input type="radio" name="list" value="满意" />满意
<input type="radio" name="list" value="不满意" />不满意
<input type="radio" name="list" value="非常差" />非常差
<br />
<input type="submit" value="submit"  id="btnSubmit" />
</form>
</body>
</html>
Copy after login

above The code uses the $('input:radio:checked').val(); method to obtain the value in the radio, and then determines whether the radio is selected by judging whether the radio is empty.

The above is the detailed content of How does jquery determine whether the radio is selected?. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!