2017-04-16 86 views
-3

我希望将regis.checksum转换为1,以便下一次block不会运行。我也是新来的stackoverflow。在此先感谢了help`JavaScript对象变量不会改变

var regis = { 
     checksum : 0, 
     username : false, 
     first_name : false, 
     password : false, 
     password_check : false 
    } 

if($("#id_username").val().trim() == ""){ 
    if(regis['checksum']==0){ 
     alert(regis.checksum) 
     $("#id_username").focus(function(){ 
      $("#error_username").text("Your email id will be created with this name"); 
      regis['checksum'] = 1 
     }) 
    }else{ 
     $("#id_username").on('input', function() { 
      alert("Change to " + this.value); 
     }); 
    } 
}` 
+0

有啥问题? –

+0

@Yogesh regis.checksum只改变本地范围的值,但我希望它改变全球regis.checksum –

+0

是你的'regis'对象在文档准备好之外声明吗? –

回答

0
(function(){ 
    var regis = { 
      checksum : 0, 
      username : false, 
      first_name : false, 
      password : false, 
      password_check : false 
     }; 


    function validate(value){ 
     if(!value){ 
     $("#error_username").text("Username field cannot be left empty"); 
     } else if(value.indexOf(" ") > -1){ 
     $("#error_username").text("Username field cannot have spaces"); 
     } else { 
     $("#error_username").text(""); 
     } 
    } 

    $("#id_username").focus(function(){ 
    if(regis.checksum === 0){ 
    console.log(regis.checksum) 
    $("#error_username").text("Your email id will be created with this name"); 
    regis.checksum = 1 
    } else { 
    validate($("#id_username").val()); 
    } 
    }); 


    $("#id_username").on('input', function() { 
    if(regis.checksum === 1){ 
     validate($("#id_username").val()); 
    } 
    }); 
})() 

http://jsbin.com/paridad/edit?html,js,console,output

+0

函数似乎进入循环 –

+0

@gauravsamant:什么是用户...? –

+0

我想创建一个注册表单。概念是,如果用户第一次关注到用户名将显示字符串“这将是你的电子邮件用户名”否则它会显示,如果该字段是空的或不规则 –