日期:2014-05-16  浏览次数:20380 次

jquery判断是否选中单选按钮的问题,请前辈们给看看
HTML code

<form>
性别
<input type="radio" name="sex"> 男
<input type="radio" name="sex"> 女
爱好
<input type="radio" name="enjoy">看书
<input type="radio" name="enjoy">逛街
<input type="radio" name="enjoy">运动
政治面貌
<input type="radio" name="zhengzhi">团员
<input type="radio" name="zhengzhi">党员
<input type="submit" value="提交">
</form>



下面是我写的jquery,当所有选项都选中的时候,我才提交表单,有一项未选中都不能提交表单,请各位看看我哪错了.怎么修改,非常感谢


JScript code


function toSubmit()
{
    var s=true;
    $(":radio").each(function(){
        
        var radio = $(this).attr("name");
        
        if($(":radio[name='"+radio+"']").attr("checked")==false)
        {
            s=false;
        }
              
    })
    if(s==false)
    {
        alert("所有项为必填!");
    }    

    return s;




------解决方案--------------------
HTML code
<!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>
    <title>无标题页</title>
    <script type="text/javascript" src="../script/jquery-1.7.js"></script>    
    <script type="text/javascript">
    window.onload=function(){
        $("#b1").click(function(){
            alert($(":radio:checked").length);
        });
    };
    </script></head>
<body>
<input type="radio" name="sex"> 男
<input type="radio" name="sex"> 女
爱好
<input type="radio" name="enjoy">看书
<input type="radio" name="enjoy">逛街
<input type="radio" name="enjoy">运动
政治面貌
<input type="radio" name="zhengzhi">团员
<input type="radio" name="zhengzhi">党员


<input type="button" id="b1" value="click me" />
</body>
</html>

------解决方案--------------------
HTML code
<!doctype html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Test</title>
    <script src="jquery-1.4.2.js"></script>
</head>
<body>
    <form>
    性别:
    <input type="radio" name="sex">
    男
    <input type="radio" name="sex">
    女<br />
    爱好:
    <input type="radio" name="enjoy">看书
    <input type="radio" name="enjoy">逛街
    <input type="radio" name="enjoy">运动<br />
    政治面貌:
    <input type="radio" name="zhengzhi">团员
    <input type="radio" name="zhengzhi">党员<br />
    <input type="submit" value="提交">
    </form>
    <script>
        function toSubmit() {
            var count = 0;
            $("input[type=radio]:checked").each(function () {
                count++;
            });
            if (count != 3) {
                alert("所有项为必填!");
            }
        }
        $(":submit").click(toSubmit);
    &