2014-01-31 3 views
1

나는 그것을 칠할 때 텍스트 상자로 변할 수있는 표 셀을 얻으려고하고 그래서 그것을 채울 수 있고 텍스트가 셀에 추가된다.Javascript getElementbyId는 한 번 작동하지만 두 번 작동하지 않습니까?

$(".cellConsigne").click(function() { 
    var numId = this.id.substring(24); 
    document.getElementById("MainContent_txtConsigne" + numId).style.display = "block"; 
    document.getElementById("MainContent_txtConsigne" + numId).focus(); 
}); 

$(".txtTab").focusout(function() { 
    var numId = this.id.substring(23); 
    document.getElementById("MainContent_cellConsigne" + numId).textContent = this.value; 
    this.style.display = "none"; 
}); 

및 세포 내 선언과 뒤의 C# 코드에서 텍스트 상자 :

내 자바 스크립트 함수가

TableCell cell17 = new TableCell(); 
cell17.ID = "cellConsigne" + i.ToString(); 
cell17.CssClass = "cellConsigne"; 

TextBox txtConsignes = new TextBox(); 
txtConsignes.ID = "txtConsigne" + i.ToString(); 
txtConsignes.CssClass = "txtTab"; 
txtConsignes.BackColor = row.BackColor; 

cell17.Controls.Add(txtConsignes); 

그래서 문제가 있음을 잘이 작품 처음으로, 나는 클릭하고, textBox 쇼와 초점을 얻었다. 텍스트를 입력 한 다음 textBox를 그대로두고 텍스트를 셀로 이동합니다. 그러나 두 번째로 셀을 클릭하면 document.getElementById("MainContent_cellConsigne" + numId)에 "참조 null 오류"가 나타납니다.

문제는 CSS 클래스 또는 스타일 수정에서 올 수 있습니까? 나는 정말로 모른다.

+2

jQuery를 사용하는 경우에는 그것에 충실해야합니다. 'getElementById'를 사용하지 않으면 좋은 시작이 될 것입니다. – CodingIntrigue

+1

디버깅을 배우십시오. 줄 뒤에'console.log (numId);'를 추가하고 가지고있는 것을 보라. 나는 그것이 당신에게 오류를 보여줄 것입니다. – epascarello

+0

또한 'getElementById' DOM 쿼리에서 개체를 저장해야하며이 작업을 두 번 수행하면 안됩니다. – timmkrause

답변

1

문제점을 발견했습니다.

내가

document.getElementById("MainContent_cellConsigne" + numId).textContent = this.value; 

그것은 내부의 컨트롤에서 셀을 비우고 그 후 셀에 텍스트를 추가 할 때. 따라서 textBox는 더 이상 셀에 없습니다.

방금 ​​셀에 레이블을 추가하고 텍스트 상자의 텍스트를 레이블에 추가하면 셀이 더 이상 비어 있지 않습니다.

관련 문제