<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,当所有选项都选中的时候,我才提交表单,有一项未选中都不能提交表单,请各位看看我哪错了.怎么修改,非常感谢
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;

解决方案 »

  1.   

    if($(":radio[name='"+radio+"']").attr("checked")==false)
            {
                s=false;
                return false;
            }
      

  2.   

    <!doctype html>
    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Test</title>
        <script src="jquery.js" ></script>
    </head>
    <body>
    <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>
    <script>
    function toSubmit(){
    var result = true;
        var radioSets = {};
        $(":radio").each(function(){
            var name = $(this).attr("name");
            if(!radioSets[name]){
             radioSets[name] = [];
            }
            radioSets[name].push($(this));
        });
        $.each(radioSets, function(i, radios){
         $.each(radios, function(j, radio){
         if(radio.prop('checked')){
         return false;
         }else if(j === radios.length - 1){
         result = false;
         alert(i+"未填!");
         }
         });
        });
        return result;
    }
    $(":submit").click(toSubmit);
    </script>
    </body>
    </html>
      

  3.   

    $(":radio[name='"+radio+"']"):找到type为radio的input标签,[attribute=value]进一步过滤,找到该属性等于value的标签.但是看你的标签里没有name为radio的标签啊.
      

  4.   

    直接判断选中按钮的个数,小于3个都不能提交
    var num=$("input[type=radio]":checkd).length;
    if(num<3)
    {
    alert("所有项为必填!");
    }
      

  5.   

    var num=$("input[type=radio]":checkd).length;这行咋报错呢
      

  6.   

    alert不出来啊,$(":radio:checked").length不对吧...
      

  7.   

    <!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>
    怎么可能会出不来
      

  8.   

    <!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);
        </script>
    </body>
    </html>
      

  9.   

    杯具了var num=$("input[type=radio]:checkd").length;