zoukankan      html  css  js  c++  java
  • JS 判断CHECKBOX复选框多选及选中项验证

    需求:多个复选框,要求选中其中一项,如果text的值为空,给出提示。如下图所示:

    html 代码如下

    <input type="checkbox" name="select_no_audit_reason" id="select_no_audit_reason" value="1">
    <input type="checkbox" name="select_no_audit_reason" id="select_no_audit_reason" value="2">
    <input type="checkbox" name="select_no_audit_reason" id="select_no_audit_reason" value="3">
    <input type="checkbox" name="select_no_audit_reason" id="select_no_audit_reason" value="4">
    <input type="checkbox" name="select_no_audit_reason" id="select_no_audit_reason" value="5">
    <input type="text" name="put_in_reason" id="put_in_reason" size="20" maxlength="20" value="<%=not_pass_remark %>" check_str="其他原因">

    现在要实现的是选择其他选项,提交的时候提示其他不能为空。

    js代码如下:

    function checkMe(){
            var audit_state=document.getElementById("select_audit_state").value;
            var index=document.form1.select_no_audit_reason
            var checked=false;
                if(audit_state==0){
                    for (i=0;i<index.length; i++){
                        if(index[i].checked==true){                
                            //break;
                            if (index[4].checked==true){
                                checked=true;
                                return (check_form(form1));
                                }
                            checked=true;
                            break;                    
                            }                        
                        }
                        if(checked==false){
                            alert("请至少选择一个不通过原因!");
                            window.history.go(-1);
                            return false;
                            }
                        }
                    }

    其中:return (check_form(form1));是用来验证input的js方法,验证代码如下。(这段代码可以根据自身需要修改为验证其他类型数据的代码)

    function check_form(form1)
    {
    
    //    return false;
           try
           {
            var aa = form1.elements; //document.forms(form1.name).elements;
            var obj = null;
            var jumpFromFor = false;
        
                for (i=0;i<aa.length;i++)    
                {
                    jumpFromFor = true;  //如果中途跳出,jumpFromFor的值将被保持为true,表示验证未通过
                    if(aa[i].attributes["check_str"]!=null && aa[i].attributes["check_str"].value!="")
                    {
                        obj = aa[i];
            
                        if(obj.value.length==0)
                        {
                            if(obj.attributes["can_empty"]!=null && obj.attributes["can_empty"].value=="Y")
                            {
                                if(i==aa.length-1){jumpFromFor = false;}
                                continue;
                            }else
                            {
                                alert("『"+obj.attributes["check_str"].value+"』不能为空,请重新输入","<br>");
                                break;
                            }
                        }
                    }    
                }
            }
        }

    OK,就到这里了!

    风雨苦痛皆营养,欲成大木柱天长。
  • 相关阅读:
    P2437 蜜蜂路线题解
    P1044 栈题解
    P1002 过河卒题解
    P1433 吃奶酪题解
    组合数公式
    P1036 选数题解
    十进制转二进制方法整理
    golang学习笔记 ---工作区与GOPATH
    golang学习笔记---闭包
    golang学习笔记---类型
  • 原文地址:https://www.cnblogs.com/acoll/p/2688997.html
Copyright © 2011-2022 走看看