2012-03-06 85 views
0

我想我的按鈕(我生成很多與asp.net內<%%標籤。)將顯示「textboxHidden」div,然後切換「響應」和「關閉」之間的按鈕值。如果我再次單擊「關閉」按鈕,我希望它再次隱藏div ...jquery .show和.toggle打開和關閉一次點擊

在現實中,當我單擊按鈕時,它會隱藏並顯示div幾次,然後隱藏「close 「按鈕」 這是爲什麼

那在div:

<div id="buttons"> 
         <span class="reply"></span><span class="reply"> 
          <input type="button" id="replyButton<%=i %>" value="Respond" onclick="showTextArea('<%=i %>')" /> 
         </span> 
        </div> 
        <div id="textboxHidden<%=i %>" style="display: none;"> 
         <textarea style="width: 500px; height: 70px;" name="forum_topic_comment<%=i %>" id="forum_topic_comment<%=i %>"></textarea> 
         <div id="hiddenSave<%=i %>" style="display: none;"> 
          <input type="button" value="Save" onclick="submitComment('<%=i %>');" /> 
         </div> 
        </div> 

那jQuery代碼:

function showTextArea(i) { 
       $("#replyButton" + i).click(function() { 
        $("#textboxHidden" + i).toggle(200); 
        $("#hiddenSave" + i).show(200); 
        $("#replyButton"+i).val('Close').toggle(); 

       }); 
      } 

感謝助手

回答

1

showTextArea()應只包含這個:

$("#textboxHidden" + i).toggle(200); 
$("#hiddenSave" + i).show(200); 
($("#replyButton"+i).val() != 'Close' ? $("#replyButton"+i).val('Close') : $("#replyButton"+i).val('Respond')) 

請參閱:http://jsfiddle.net/hdfku/2/

+0

謝謝!有用!。但是如何讓「關閉」再次隱藏時又變爲「響應」? – thormayer 2012-03-06 12:40:11

+1

@thormayer:代碼已更新。 – codef0rmer 2012-03-06 13:01:17

1

當然,看看你在做什麼。您正在爲每個按鈕分配一個onclick屬性。在每次點擊時,它都會調用showTextArea函數,每次單擊該按鈕時都會添加一個新的(!)點擊處理程序。所有這些點擊處理程序都會在下一次按鈕點擊時運行。隨着每次點擊,你會得到更多的點擊處理程序與

<input class="showTextArea" type="button" id="replyButton<%=i %>" value="Respond" /> 

並更換JS:

1

輸入刪除的onclick並添加一個類 「showTextArea」

$(".showTextArea").click(function() { 
    i = $(this).attr('id').replace("replyButton","") 
    $("#textboxHidden" + i).toggle(200); 
    $("#hiddenSave" + i).show(200); 
    $("#replyButton"+i).val('Close').toggle(); 
}) 

工作對我罰款

+0

當我這樣做時,它不會做任何事情,當我點擊它。 – thormayer 2012-03-06 12:08:00