2012-02-10 5 views
2

을 선택하기 내 문제는 내 테이블 (MainContent_tb)가 고정되지 않는 것입니다행 ID를 선택 얻을 나는 JQuery와 아래 사용하고 행 ID

<script src="../Scripts/jquery-1.4.1.min.js" type="text/javascript"></script> 
     <script type="text/javascript"> 
      //MainContent_tbl 
      $(document).ready(function() { 
       $('#MainContent_tbl tr').click(function (event) { 
        alert(this.id); //trying to alert id of the clicked row   

       }); 
      }); 



    </script> 

, 내가 어떤 제안이나 발언이 것, 변수로이를 전달해야 감사하겠습니다. 작동해야

var tableId = 'MainContent_tbl'; 
$('#' + tableId + ' tr').click(function(event) { 
    alert(this.id); 
} 

같은

+0

따라서 테이블 * 함량 * 변수 또는 표의 자료 * * 동적 원소? –

+0

동적으로 행을 추가/제거하는 것을 이해합니다. 그런 다음 행을 추가 할 때 이벤트 처리기를 새 행과 연결해야합니다. 'jQuery.live'를 사용하는 것이 좋지만 그렇게하지는 않을 것입니다. 사용하지 마십시오. 사용되지 않습니다. – Oybek

+0

@SurrealDreams 좋은 질문입니다. ID가 내 대답에서 역동적 인 부분이라고 가정했으나 정확하지 않을 수 있습니다. –

답변

1
$(document).ready(function() { 
    $('#MainContent_tbl tr').click(function (event) { 
     var elID = $(this).attr('id'); 
     alert(elID); 
    }); 
    }); 

이 당신을 위해 무엇을 찾고 있습니다!

+0

그것은 매력처럼 작동, 많은 Jannis 정말 감사합니다 감사합니다 – ChandaMAMA

0

뭔가. 하드 코딩되지 않고 단순히 변수를 사용하여 선택기 문자열을 만듭니다.

0

노력이

$('#'+table_id_as_variablle+' tr').click(function (event) { 
     alert(this.id); //trying to alert id of the clicked row   
    }); 
0

delegate을 사용하고 처리기를 tr이라는 선택자가있는 문서에 첨부 할 수 있습니다. 이렇게하면 클릭 핸들러가 하나만 연결되며 테이블 ID에 대해 신경 쓸 필요가 없습니다. 모든 테이블에서 작동합니다. 이 시도.

 $(document).ready(function() { 
      $(document).delegate('tr', 'click', function (event) { 
       alert(this.id); //trying to alert id of the clicked row   
      }); 
     }); 

참조 : .delegate()