2011-08-15 9 views
-3

이 오류의 원인은 무엇입니까? element.value를 함수에 전달해야합니까? JSP로 설정하는 방법을 알고 있지만 JavaScript에서는 설정하지 않습니다. 값을 다른 함수로 전달하는 데 문제가 있습니다. 이것이 당신의 characterCounterEdit 함수가 호출 theonly 장소 인 경우'value.length'가 null이거나 객체가 아닙니다.

function editCommentToggle(id) 
{ 
    theRow = document.getElementById("id"+id); 
    //user = theRow.cells[0].innerHTML; 
    //date = theRow.cells[1].innerHTML; 
    com = theRow.cells[2].innerHTML; 


    idx = 2; 
    maxlength = 250; 
    // Comment field 

    cell = theRow.cells[idx]; 
    while(cell.childNodes.length > 0) 
     cell.removeChild(cell.childNodes[0]); 
    element = document.createElement("textarea"); 
    element.id="comments-"+id; 
    element.rows="3"; 
    element.value = com; 
    element.style.width = "400px"; 
    element.maxLength = "250"; 
    element.onfocus = element.onkeydown = element.onkeyup = function(){ 
      return characterCounterEdit(undefined, maxlength, element); 
    }; 
    cell.appendChild(element); 



function characterCounterEdit(id, maxLen, inputElement) 
{ 
    spanId = document.getElementById(id); 

    if (spanId) 
    { 
     // Update the counter the user sees. 
     var whatIsLeft = maxLen - inputElement.value.length; 

     if (whatIsLeft < 0) whatIsLeft = 0; 
     spanId.innerText = whatIsLeft; 
    } 

    // Restrict user from entering more than the maxlen. 
**ERROR HERE-->>>** if (inputElement.value.length > maxLen) 
    { 
     inputElement.value = inputElement.value.substring(0, maxLen); 
    } 
} 
+1

무엇이 질문입니까? 너 뭐 해봤 니? 우리는 자동화 된 코드 디버깅 기계가 아닙니다. –

+0

나는 그 자체로 funtion에 "element"를 전달하려고 시도했다. –

+1

적어도 영어 문장이 최소한으로 구성된 질문으로 돌아와주십시오. 가급적 오타가 있거나 구두점이없는 대신에주의와주의를 기울여야합니다. –

답변

2

이 변화 시도 ...

element.onfocus = element.onkeydown = element.onkeyup = function(){return characterCounterEdit(undefined, maxlength, element);}; 

this ...

element.onfocus = element.onkeydown = element.onkeyup = function(){return characterCounterEdit(undefined, maxlength, this);}; 
                                ^^^^ 

또는 tar 이벤트를 사용해 볼 수도 있습니다. 여기에서 설명하는 get/sourceElement - characterCounterEdit 함수에 http://www.quirksmode.org/js/events_properties.html -이 있습니다.

+0

헤이 버디. –

+0

+1 : 좋은 장소 안토니! –

+0

문제가 해결되어 기쁩니다. 몇몇 사람들이이 질문을 조금 빨리 끝내고 실제로 문제를 물어볼 수 있도록 문제를 해결할 기회를주지 않았습니다. 다행히도 양면에서 배운 몇 가지 교훈 :) –

1
element.onfocus = element.onkeydown = element.onkeyup = function(){return characterCounterEdit(undefined, maxlength, element);}; 

, 다음 시간에 초점/이벤트가 트리거를 keyDown /의 keyup, element 가장 가능성이없는 아무튼 다른 값을 할당 된 .value 특성. 대신 시간에 더 이상 존재하지 않을 수있는 (elementmaxlength 트리거되는 이벤트 (들)을 통과

, 이렇게 :

element.maxlength = maxlength 
element.onfocus = element.onkeydown = element.onkeyup = function(){return characterCounterEdit();}; 

function characterCounterEdit() { 
    .... 
    var whatIsLeft = maxLen - this.value.length; 
            ^^^^--- use 'this' instead. 
    .... 
    if (this.value.length > this.maxlength) ... 
    .... 

} 
+0

헤이 버디, 고맙습니다. 그저 요소를 'this'로 바꿨습니다. 임 또 다른 방법이 있는지 ... 당신의 방법을 시도해 볼께요 ... 고맙습니다! –

관련 문제