2012-03-06 83 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