JQuery判断radio是否选中,获取选中值

时间:2023-02-10 19:34:24

/*---------------------------JQuery判断radio是否选中,获取选中值----------------------------------*/

其他对radio操作功能,以后在添加。直接上代码,别忘记引用JQuery包

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