2013-08-01 2 views
-5

javascript 객체의 속성에서 keyup 이벤트에 값을 할당하고 있습니다. 그것은 콘솔에 null을 인쇄 중입니다. 바디자바 스크립트에서 Null 오류가 발생합니다.

<div class="first"> 
    <input type="text" id="val_00_01" /> 
</div> 
<div class="two"> 
    <input type="text" id="val_00_02"/> 
</div> 
<script type="text/javascript"> 
    var first= new check('.first'); 
    var two= new check('.two'); 
    first.touch() 
    two.touch() 
</script> 
+3

질문을하고 코드를 포맷하는 것을 잊었습니다. – MightyPork

+1

코드가 깨지는 위치 또는 작동 할 때의 시나리오를 제공하지 않았습니다. – NoLifeKing

답변

1

function check(id){ 
    var me= this; 
    this.ID= null; 
    this.matchName= this.ID.substr(this.ID.lastIndexOf('_')); 
    this.first= function(){ 
    alert(me.matchName) 
} 

this.second= function(){ 
    alert(1) 
} 

this.touch= function(){ 
    $(id).find('input').keyup(function(e){ 
    me.ID= this.id;; 
    if(e.keyCode==13){ 
     id.indexOf('first')>-1? me.first(): me.second(); 
    } 
    })} 
} 

음이 하나 개의 깨진 부분 (이것은 의도적 아니라면?)

이 속성에 액세스

this.ID= null; 

시도를 null로 ID 속성을 설정 당신 null와 동등하게 설정된다

this.matchName= this.ID.substr(this.ID.lastIndexOf('_')); 

이 코드는 Check 클래스의 초기화 (생성자)에서 실행 중이며 오류가 발생합니다.


는 여기에 내가 눈 출혈을 일으킬 그렇게하지 않는 것이 좋습니다 서식, 당신이 원하는 무슨 생각입니다.

// capitalize first letter of class name 
function Check(className){ // use a prop/descriptive parameter name 
    var me = this; // set your variable "me" to class instanced 
    this.ID = null; // INITIALIZE your ID and matchName variables 
    this.matchName = null; 

    this.first = function(){ 
     alert(me.matchName) 
    }; 
    this.second = function(){ 
     alert(1) 
    }; 
    this.touch = function(){ 
     // get the element with class=className 
     // find the input inside of it 
     // set an onkeyup handler to the input element 
     $(className).find('input').keyup(function(e){ 
      me.ID = this.id;; // set the ID variable to the INPUT element's ID property 
      // set matchName to the last part of the input's ID property 
      // matchName will be "01" or "02" in this case 
      me.matchName = this.ID.split("_")[this.ID.split("_").length - 1]; 
      if(e.keyCode==13){ 
       id.indexOf('first') > -1 ? me.first(): me.second(); 
      } 
     }); 
    }; 
} 
... 
var first = new Check('.first'); 
var two = new Check('.two'); 
first.touch(); 
two.touch(); 
+0

Enter 키를 누르면 ID가 – Carlos

+0

@ amit으로 채워집니다. null로 변경하고 다음 줄을 사용하려고 할 때 어떤 종류의 마법이 바뀌나요? – JJJ

+0

@lastCoder : onkeyup 이벤트 me.ID = this.id; – Carlos

관련 문제