设为首页收藏本站

就爱编程论坛

 找回密码
 注册

人人连接登陆

无需注册,直接登录

用新浪微博连接

一步搞定

QQ登录

只需一步,快速开始

查看: 531|回复: 1
打印 上一主题 下一主题

js验证web单选框 [复制链接]

Rank: 9Rank: 9Rank: 9

  • TA的每日心情
    无聊
    2025-5-27 03:37:20
  • 签到天数: 366 天

    [LV.9]以坛为家II

    论坛先锋 学习至圣 荣誉成员 论坛元老 活跃之星 终极领袖

    我玩的应用:

    跳转到指定楼层
    楼主
    发表于 2011-3-30 03:41:01 |只看该作者 |倒序浏览

    <script   language= "JavaScript ">

    function   FormCheck()
    {   

    if(!document.form1.a.checked)
    {
    alert( "222 ");
    return   false;
    }
    }
    </script>

      <FORM     name= "form1 "   action= "register.asp "   method=post      >
       
            <input   type= "radio "   name= "a "   value= "d ">
        <input   type= "radio "   name= "a "   value= "c ">
       
            <input   type= "submit "   name= "Submit "   value= "sub ">
          
    </form>

    我用上面代码强制用户选择单选框,可即便选择了单选框还要求用户选,请问如何解决?
    ================================================
    不是这样的,document.form1.a取到的是一个数组,不是一个radioBox,所以会出错,使用下面这个方法来取

    <script   language= "javascript ">
    /**
      *get   the   value   of   a   group   of   radiobox
      *@param   radioBox   the   object   of   radioBox
      *@return   the   value   of   the   radioBox   checked   or   false   for   none   checked
      *@example   var   value   =   getRadioValue(this.form1.radioGroup);
      */
    function   getRadioValue(radioBox){
            try{
                    if(radioBox   ==   null){
                            return   false;
                    }
                    var   length   =   radioBox.length;
                    if(length   ==   null){
                            if(radioBox.checked)
                                    return   radioBox.value;
          else
                  return   false;
                    }else{
                            for(i   =   0;   i   <   length;   i++)   {
                                  if(radioBox[i].checked)
                                          return   radioBox[i].value;
          }
                            return   false;
                    }
            }catch(e)   {
                    return   false;
            }
    }   

    function   FormCheck(){   
            if(!getRadioValue(document.form1.a)){
    alert( "222 ");
    return   false;
            }
    }

    </script>
    分享到: QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    分享分享0 收藏收藏0 支持支持0 反对反对0 分享到人人 转发到微博
    [img=http://mail.qq.com/cgi-bin/qm_share?t=qm_mailme&email=fRUcHhYWGAQ9GxIFEBwUEVMeEhA]http://rescdn.qqmail.com/zh_CN/htmledition/images/function/qm_open/ico_mailme_02.png[/img]

    使用道具 举报

    Rank: 9Rank: 9Rank: 9

  • TA的每日心情
    无聊
    2025-5-27 03:37:20
  • 签到天数: 366 天

    [LV.9]以坛为家II

    论坛先锋 学习至圣 荣誉成员 论坛元老 活跃之星 终极领袖

    沙发
    发表于 2011-3-31 12:34:16 |只看该作者
    [img=http://mail.qq.com/cgi-bin/qm_share?t=qm_mailme&email=fRUcHhYWGAQ9GxIFEBwUEVMeEhA]http://rescdn.qqmail.com/zh_CN/htmledition/images/function/qm_open/ico_mailme_02.png[/img]

    使用道具 举报

    您需要登录后才可以回帖 登录 | 注册 人人连接登陆

    晴云孤魂's Blog|就爱编程搜帖|手机版|Archiver|就爱编程论坛     

    GMT+8, 2025-7-2 14:11 , Processed in 0.096269 second(s), 28 queries .

    Powered by Discuz! X2

    © 2001-2011 Comsenz Inc.

    回顶部