2010-01-10 4 views
2

높음!

내가하고 싶은 일은 다음과 같습니다. 나는 onclick이있는 테이블을 짝수 행의 테이블에있는 링크에 연결했습니다. 홀수 행은 모두 숨겨져 있습니다. 해당 링크를 클릭하면 홀수 행이 표시되고 데이터가 해당 행에로드됩니다. 잘 작동합니다.

이제 내가하고 싶은 것은 해당 프로세스가 완료 될 때마다 해당 링크에 새로운 클릭 기능을 추가하여 행을 다시 숨겨야한다는 것입니다. 일종의 토글 (toggle)과 비슷하지만, 몇 가지 기능 만 추가하면 표시/숨기기 기능이 있습니다. 다음 코드를 사용하여 작업하려고했지만 작동하지 않습니다.

나는 아주 기본적인 것을 놓치거나 jquery를 잘 모르고있다. (몇 주 전에 시작했기 때문에 가능하다.)

$(document).ready(function(){ 

    // The version icons 
    $("a.version").click(function() { 
     var sLink = $(this).attr("href"); 
     var nexttr = $(this).parent().parent().next("tr.version"); 
     var nexttrtd = nexttr.find("td:first"); 
     $.get(sLink, function(sData){ 
      nexttrtd.html(sData); 
      nexttr.show(); 
     }); 

     $(this).click(function(){ 
      attachHideMyChildren(); 
     }); 

     return false; 
    }); 
}); 

function attachShowMyChildren() 
{ 
    var sLink = $(this).attr("href"); 
    var nexttr = $(this).parent().parent().next("tr.version"); 
    var nexttrtd = nexttr.find("td:first"); 
    $.get(sLink, function(sData){ 
     nexttrtd.html(sData); 
     nexttr.show(); 
    }); 
    $(this).click(function(){ 
     attachHideMyChildren(); 
    }); 
    return false; 
} 

function attachHideMyChildren() 
{ 
    $(this).parent().parent().next("tr.version").hide(); 
    $(this).click(function(){ 
     attachShowMyChildren(); 
    }); 
} 

그것은 테이블 행을 열고 다시 행을 종료하는 기능을 부착하지 않는 한 다음 데이터를 삽입하지만. 어떻게 이런 일이 일어날 수 있습니까?

아이디어가 있으십니까?

답변

5

문제는 이것이다 :

당신이 방법 thiswindow이되는 함수를 호출
$(this).click(function(){ 
    attachHideMyChildren(); 
}); 

. 대신 이렇게 :

$(this).click(attachHideMyChildren); 

을 또한, 기존의 것들을 제거하지 않고 click() 핸들러를 추가하고 있습니다.

그렇게하는 것이 더 쉬운 방법입니다.

$("a.version").click(function() { 
    var next = $(this).closest("tr").next(); 
    if (next.is(":hidden")) { 
    $.get($(this).attr("href"), function(sData) { 
     next.find("td:first").html(sData); 
     next.show(); 
    }); 
    } else { 
    next.hide(); 
    } 
    return false; 
}); 

해야합니다.

+0

거룩한 쓰레기! 그게 그랬어. 고마워. –