现在做一个调查问卷,从后台循环取出radio单选问题,怎么验证这些radio必须选择,如果不选择提示哪个没有选中,我现在用jquery怎么实现呢,因为对jquery不熟悉,所以希望大家可以帮忙,谢谢

解决方案 »

  1.   

    $("[name=这组单选按钮的名字]:radio").each(function(){
       if($(this).attr("checked"))
       {
           //这里是被选中的    
       }
    });
      

  2.   

    哈哈,用JQuery写代码很有艺术感。
      

  3.   

    give you a example.
    in the example, if you not choice the sex field, when you click the submit button, will appear the alert info and the sex field will add the red border.
    the code below is: <div>
            <span>UserName</span>
            <div><input type="text" id="username" /></div>
        </div>
        <div>
            <fieldset>
                <legend>Sex:</legend>
                <input type="radio" name="sex" value="0" id="man" /><label for="man">Man</label>
                <input type="radio" name="sex" value="1" id="women" /><label for="women">Women</label>
            </fieldset>
        </div>
        <div>
                <input type="button" value="Submit" id="Submit" />
        </div> <script type="text/javascript" src="JS/jquery-1.4.4.js"></script>
        <script type="text/javascript">
            var sex = "";
            $(function() {
                $("input#Submit").click(function() {
                    sex = "";
                    sex = $("input[type='radio'][name='sex']:checked").val();
                    if (sex == "" || sex==null) {
                        alert("you should choice the sex!");
                        $("fieldset").parent().css({ "border": "solid 1px red" });
                    }
                });
            });
        </script>just a example. the more functions, which you can add.
      

  4.   

    单个radio的验证我已经解决了,现在就是多个radio来验证,这些数据都是从后台读取出来,100个数据,现在分成十页,每页都要单独验证。
    比如 
    <input type="radio" name="radio1" value="0" id="man" />Man
    <input type="radio" name="radio1" value="1" id="women" />Women<input type="radio" name="radio2" value="0" id="man" />Man
    <input type="radio" name="radio2" value="1" id="women" />Women<input type="radio" name="radio3" value="0" id="man" />Man
    <input type="radio" name="radio3" value="1" id="women" />Women.
    .
    .
    <input type="radio" name="radio10" value="0" id="man" />Man
    <input type="radio" name="radio10" value="1" id="women" />Women是不是要写10次验证啊
      

  5.   

    在你用的数据控件里插入验证,或者把你的控件放在一个div下,用js取到div里的所有input加验证
      

  6.   


    <!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="/js/jquery-1.3.2.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){
                $("#vali").click(function(){
                    $("input[type='radio']").each(function(){
                        if(!$(this).attr("checked")){
                            alert($(this).next().html()+"为必选项,请选择");
                        }
                    })
                })
            })
        </script>
    </head>
    <body>
    <input type="radio" name="radio1" value="0" id="man" /><span>男人</span>
    <input type="radio" name="radio1" value="1" id="women" /><span>女人</span>
    <input type="radio" name="radio1" value="2" id="nopeople" /><span>非男人非女人</span>
    <a href="#" id="vali">验证</a>
    </body>
    </html>
      

  7.   

    JQuery操作checkbox、radio