V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
IdJoel
V2EX  ›  jQuery

jquery 表单验证 each 为什么不能 key 变量

  •  
  •   IdJoel · 2016-10-25 17:35:49 +08:00 · 1920 次点击
    这是一个创建于 2730 天前的主题,其中的信息可能已经有所发展或是发生改变。
    $this.find(":input").bind("click",function (){
        var _this = $(this).attr("name")
        var _this_val = $(this).val()
        var num_text = /^\d{1,16}$/;
        var password = /^[\@A-Za-z0-9\!\#\$\%\^\&\*\.\~]{6,22}$/;
        var _this_name = {num_text:"username", password:"password", three:"cerificationcode"}; 
        $.each(_this_name,function(key,val){    
        if(_this == val){
            console.log(key)
            if(key.test(_this_val)){
                alert(" 对了对了");
            }else{
                alert("错误错误了");
            }
        }
        })
    })	
    

    好像是第二个 if 的 key 不被 jquery 识别变成变量,浏览器就一直在报错

    html:52 Uncaught TypeError: key.test is not a function(…)
    
    bdbai
        1
    bdbai  
       2016-10-25 20:30:10 +08:00 via Android
    key 是字符串吧
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1286 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 23:25 · PVG 07:25 · LAX 16:25 · JFK 19:25
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.