IdJoel
V2EX  ›  jQuery

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

  •  
  •   IdJoel · Oct 25, 2016 · 2537 views
    This topic created in 3547 days ago, the information mentioned may be changed or developed.
    $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  
       Oct 25, 2016 via Android
    key 是字符串吧
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3614 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 135ms · UTC 04:48 · PVG 12:48 · LAX 21:48 · JFK 00:48
    ♥ Do have faith in what you're doing.