2012-02-05 5 views
-1

내 질문입니다! 한 테이블에서 다른 테이블로 내용을 복사 할 때이 작업을 수행 할 때 각 테이블 행에 첨부 된 driverselect 대신 talentselect로 변경할 함수 이름이 필요합니다. 변수 값을 파싱 할 필요가 있습니다. 아무도 이것으로 나를 도울 수 있는지 궁금해. JQuery를 사용하여 이벤트를 OnClick을 사용하지 않고 바인딩해야한다는 것을 알고 있지만 지금은 OnClicks를 사용하여이 작업을 수행 할 수있는 솔루션이 필요합니다.JQuery 변경 onclick 함수 이름

많은 감사!

테이블의 복사

<table id="driverselectiontable" cellspacing="0"> 
<tr class="chosen" onclick="return driverselect(this, value, value);"> 
<td>driver</td></tr> 
<tr class="odd" onclick="return driverselect(this, value, value);"> 
<td>driver</td></tr> 
<tr class="even" onclick="return driverselect(this, value, value);"> 
<td>driver</td></tr> 
<tr class="chosen" onclick="return driverselect(this, value, value);"> 
<td>driver</td></tr> 
<tr class="even" onclick="return driverselect(this, value, value);"> 
<td>driver</td></tr> 
</table> 

<table id="talentselectiontable" cellspacing="0"> 
</table> 


$("#talentselectiontable").html($("#driverselectiontable .chosen").clone(true).attr("class","odd")); 

그래서 기본적으로 내가라는 이름의 클래스가 "선택"이 테이블의 모든 행을 복사하고 있지만 이렇게에 내가 "talentselect"로 함수 호출 이름을 변경해야 . 그러나 테이블의 각 행에는 PHP로 할당 된 다른 매개 변수가 파싱됩니다.

나는이 코드 조각을 시도했지만 여전히

$("#talentselectiontable tr").attr("onclick").replace("driverselect", "talentselect"); 
+0

정보를 충분하게 게시하지 않았습니다. 그리고 네, 그 인라인 자바 스크립트를 제거해야합니다. – Sparky

+1

이미'this'를 함수의 param으로 사용한다면 왜 이름을 변경해야합니까? –

+1

Sparky672가 말했듯이, 우리는 당신을 돕기 위해 좀 더 많은 정보가 필요합니다. 특히, onclick 특성을 보유하고있는 테이블 행을 구성하는 HTML을 약간 보는 것이 도움이됩니다. 그 이유는 변화가 필요한 'onclick'이 어디에 있는지를 알아야하기 때문입니다. –

답변

1

나는 테이블의 실제 HTML을 볼 수 없습니다 작동하지 않습니다, 그래서 이것은 약간 잘못되었을 수 있습니다,하지만 당신은 시작한다.

$("#talentselectiontable tr").attr("onclick", "return talentselect(this, value, value);"); 

인라인 onclick 속성 대신 이벤트 처리기를 사용하면이 모든 문제가 실제로 훨씬 쉽게됩니다. 그런 다음 jQuery live() 함수 (http://api.jquery.com/live/ 참조)를 사용하면 JQuery가 함수 변경을 처리하게됩니다. 당신의 복제 코드가 호출되는 것을 보장하기 위해 다음

$("#driverselectiontable tr").live('click', driverselect); 
$("#talentselectiontable tr").live('click', talentselect); 

그리고 어떤 코드 : 귀하의 솔루션은 다음과 같이 보일 것입니다.

편집 : 코멘트에 응답에서, 이것이처럼 보이는 당신이있어 무엇 후 : 당신이 가야한다

var clickval = $("#talentselectiontable tr").attr("onclick"); 
$("#talentselectiontable tr").attr("onclick", clickval.replace('driverselect', 'talentselect')); 

.

더 나은 JavaScript 실습을 위해 다른 접근 방식을 전적으로 권장합니다. 매개 변수 값을 'onclick'속성에 저장하는 대신 데이터 속성에 저장하십시오. 따라서 HTML은 다음과 같이 보입니다.

<table id="driverselectiontable" cellspacing="0"> 
<tr class="chosen" data-myparam1="value" data-myparam2="value"><td>driver</td></tr> 

등등.

function driverselect() { 
    var row = $(this), 
     param1 = row.attr('data-myparam1'), 
     param2 = row.attr('data-myparam2'); 

    // rest of the code goes here 

} 

마크 업 청소기 될 것이며, 전술 한 바와 같이 당신은 live()의 jQuery 기능을 사용할 수 있습니다 : 당신은 다음의 값을 구문 분석 JQuery와 사용할 수 있습니다.

+0

hmmm 네, 유일한 문제는 각 행 매개 변수 값이 다르다는 것입니다.하지만이 방법을 시도했지만 작동하지 않습니다. $ ("# talentselectiontable tr"). attr ("onclick"). replace ("driverselect", "talentselect"); – user969729

+0

참고 사항 : 새로운 방식으로 익숙해 져야합니다 ... [jQuery'.on()'] (http://api.jquery.com/on/) – Sparky

+0

정보에 대해 많은 감사를드립니다. HTML5 데이터 속성을 사용하는 것에 대해 생각하고 있지만 IE7, IE8과 호환됩니까? 그리고 다른 대안이 있습니까? 친절한 답변들 – user969729