2011-01-01 2 views
0


다음과 같은 html 파일이 있습니다.jQuery 동적 텍스트 (또는 데이터) 클릭 변경 문제

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<style> 
.style1 { 
    background-color: #c3d9ff; 
    font-family:arial,sans-serif; 
} 
.style2 { 
    text-align: center; 
    font-weight: bold; 
} 
.style3 { 
    background-color: #FFFFFF; 
    font-family:arial,sans-serif; 
    text-align: center; 
    font-weight: bold; 
} 
.style4 { 
    background-color: #FFFFFF; 
    font-family:arial,sans-serif; 
    text-align: left; 
} 
body { 
font-family:Verdana, Arial, Helvetica, sans-serif; 
font-size:15px; 
background-color: ; 
} 
.action_button { 
font-weight:bold; 
float:right; 
} 
</style> 
<script type="text/javascript" src="jquery-1.4.4.min.js"></script> 
<script type="text/javascript"> 
$(function() 
{ 
    $(".action_button").click(function() 
    { 
    var id = $(this).attr("id"); 
    var dataString = 'id='+ id ; 
    var parent = $(this).parent().parent(); 
    $.ajax({ 
     type: "POST", 
     url: "action.php", 
     data: dataString, 
     cache: false, 
     success: function() 
     { 
      if(id % 2) 
      { 
       parent.fadeOut('fast', function() {$(this).fadeOut();$(this).fadeIn();}); 
      } 
      else 
      { 
       parent.fadeOut('fast', function() {$(this).fadeOut();$(this).fadeIn();}); 
      } 
     } 
    }); 
    return false; 
    } 
);}); 
$(document).ready(function() { 
    $('.action_button').click(function() { 
     $(this).text($(this).text() == 'Inactivate' ? 'Activate' : 'Inactivate'); 
     var $col5 = $(this).closest('tr').find('.clickme2'); 
     $col5.text($col5.text() == 'Inactive' ? 'Active' : 'Inactive'); 
     return false; 
    }); 
}); 
</script> 
</head> 
<body> 
<table style="width: 90%" align="center" class="style1"> 
<tr> 
     <td colspan="7" class="style2">MANAGER</td> 
    </tr> 
    <tr> 
    <td class="style3" style="width: 139px">Col1</td> 
    <td class="style3" style="width: 139px">Col2</td> 
    <td class="style3" style="width: 139px">Col3</td> 
    <td class="style3" style="width: 139px">Col4</td> 
    <td class="style3" style="width: 139px">Col5</td> 
    <td class="style3" style="width: 200px">Col6</td> 
    <td class="style3" style="">Action</td> 
</tr> 
</table> 
<td id="main" class="main"> 
<td class="update"> 
<table style="width: 90%" align="center" class="style1"> 
    <tr> 
    <td class="style4" style="width: 139px">DataA1</td> 
    <td class="style4" style="width: 139px">DataA2</td> 
    <td class="style4" style="width: 139px">DataA3</td> 
    <td class="style4" style="width: 139px">DataA4</td> 
     <td class="style4 clickme2" style="width: 139px">Inactive</td> 
    <td class="style4" style="width: 200px">DataA6</td> 
    <td> 
      <button href="#" id="DataA1" class="action_button" style="width:80px;height:"> 
      Activate</button> 
      </td> 
    </tr> 
    <tr> 
    <td class="style4" style="width: 139px">DataB1</td> 
    <td class="style4" style="width: 139px">DataB2</td> 
    <td class="style4" style="width: 139px">DataB3</td> 
    <td class="style4" style="width: 139px">DataB4</td> 
     <td class="style4 clickme2" style="width: 139px">Inactive</td> 
    <td class="style4" style="width: 200px">DataB6</td> 
    <td> 
      <button href="#" id="DataA1" class="action_button" style="width:80px;height:"> 
      Activate</button> 
      </td> 
    </tr> 
    <tr> 
    <td class="style4" style="width: 139px">DataC1</td> 
    <td class="style4" style="width: 139px">DataC2</td> 
    <td class="style4" style="width: 139px">DataC3</td> 
    <td class="style4" style="width: 139px">DataC4</td> 
     <td class="style4 clickme2" style="width: 139px">Active</td> 
    <td class="style4" style="width: 200px">DataC6</td> 
    <td> 
      <button href="#" id="DataA1" class="action_button" style="width:80px;height:"> 
      Inactivate</button> 
      </td> 
    </tr> 
    <tr> 
    <td class="style4" style="width: 139px">DataD1</td> 
    <td class="style4" style="width: 139px">DataD2</td> 
    <td class="style4" style="width: 139px">DataD3</td> 
    <td class="style4" style="width: 139px">DataD4</td> 
     <td class="style4 clickme2" style="width: 139px">Active</td> 
    <td class="style4" style="width: 200px">DataD6</td> 
    <td> 
      <button href="#" id="DataA1" class="action_button" style="width:80px;height:"> 
      Inactivate</button> 
      </td> 
    </tr> 
    <tr> 
    <td class="style4" style="width: 139px">DataE1</td> 
    <td class="style4" style="width: 139px">DataE2</td> 
    <td class="style4" style="width: 139px">DataE3</td> 
    <td class="style4" style="width: 139px">DataE4</td> 
     <td class="style4 clickme2" style="width: 139px">Inactive</td> 
    <td class="style4" style="width: 200px">DataE6</td> 
    <td> 
      <button href="#" id="DataA1" class="action_button" style="width:80px;height:"> 
      Activate</button> 
      </td> 
    </tr> 
</table> 
</td> 
</td> 
</body> 
</html> 

페이지에는 각 행 끝에 버튼이있는 표가 있습니다. 버튼에는 텍스트를 변경하는 기능이 있습니다 (즉, 사용자가 버튼을 클릭하면 버튼의 텍스트와 col5가 미리 정의 된대로 스왑 됨). "활성화"버튼을 클릭하면 버튼 텍스트가 "비활성"으로 변경되고 col5 텍스트가 "활성"으로 변경됩니다. 또한, "비활성화"버튼을 클릭하면 버튼 텍스트가 "활성화"되고 col5 텍스트가 "비활성"으로 변경됩니다. 그러나 "Inactivate"버튼은 잘하지 못합니다.
PHP 파일 action.php는 아무 것도 인쇄하지 않기 때문에 여기서는 관련이 없습니다. 사전

에서

덕분에 blasteralfred :

답변

0

비활성화 버튼 문제는 text() 함수로 트림을 사용하십시오. 즉

$(document).ready(function() { 
    $('.action_button').click(function() { 
       $(this).text($.trim($(this).text()) == 'Inactivate' ? 'Activate' : 'Inactivate'); 
     var $col5 = $(this).closest('tr').find('.clickme2'); 
     $col5.text($.trim($col5.text()) == 'Inactive' ? 'Active' : 'Inactive'); 
     return false; 
    }); 
}); 

제이콥이 언급 한 수정 사항에 유의하십시오.

편집 : $ .trim 대신 String.trim

+0

을 사용하도록 업데이트 당신은 * * .trim ('사용해서는 안)',하지만'$ .trim()'대신,이 같은'$ .trim ($ (this) .text())', IE <9에는'String.trim()'함수가 없기 때문에 위의 응답은 실패합니다. –

+0

감사합니다. Nick. 게시물을 업데이트했습니다. – Chandu

1

당신은 success 핸들러 내부에 클릭 된 요소에 액세스하기 위해 당신의 주변 click 핸들러에서 this에 대한 참조를 저장해야합니다 thissuccess 핸들러는 이 아니며이 아니며 click 핸들러와 동일한 this입니다.

저는 이것을 self이라고 부릅니다. 당신이 원하는대로 부를 수 있습니다.

또한 success 핸들러에서 이전 애니메이션이 완료되면 후속 애니메이션을 수행해야하므로 해당 코드가 차례대로 페이드 시퀀스를 연결하도록 수정했습니다.

$(".action_button").click(function() { 
    var id = $(this).attr("id"); 
    var self = this; 
    var dataString = 'id='+ id ; 
    var parent = $(this).parent().parent(); 
    $.ajax({ 
     type: "POST", 
     url: "action.php", 
     data: dataString, 
     cache: false, 
     success: function() 
     { 
      if(id % 2) 
      { 
       parent.fadeOut('fast', function() { 
       $(self).fadeOut('normal', function() { 
        $(self).fadeIn(); 
       }); 
       }); 
      } 
      else 
      { 
       parent.fadeOut('fast', function() { 
       $(self).fadeIn('normal', function() { 
        $(self).fadeOut(); 
       }); 
       }); 
      } 
     } 
    }); 
});