javascript - 单击按钮时使用 JQuery 显示/隐藏某些 Div

标签 javascript jquery asp.net button toggle

我试图在单击按钮时隐藏/显示/切换 div。我正在使用 ASP.NET,所有内容都在 ASP:Datalist 内。

我可以正确显示或隐藏 div。但是,它会打开所有 div,而不仅仅是选择按钮的 div。我试图显示/隐藏的 div 是 .content

如何只打开按钮所属的div?

JSFiddle - 这是问题的示例 http://jsfiddle.net/kMEre/

脚本:

<script type="text/javascript">
    jQuery(document).ready(function () {
        jQuery(".content").hide();
    });
</script>
<script type="text/javascript">
    jQuery(document).ready(function () {
        jQuery('#man').live('click', function (event) {
           jQuery('.content').toggle('show');
        });
    });
</script> 

数据列表 (ASP.NET)

<asp:DataList runat="server" id="dgQuestionnaire" DataKeyField="QuestionID" >
    <ItemTemplate>
        <div class="question_box">
            <p class="small_bold">Question <asp:Label ID="lblOrder" runat="server" Text='<%# Container.ItemIndex  + 1 %>'></asp:Label></p>
            <div class="Questions">
               <div class="heading">
                   <asp:HiddenField ID="hiddenQuestionID" runat="server" Value='<%# Eval("QuestionID") %>' />
                   <asp:TextBox runat="server" ID="tbQuestionName" Text='<%# Eval("QuestionText") %>' CssClass="form" Width="300px"></asp:TextBox>
                   <input type='button' id='man' value='hide/show'>
               </div> <!-- end heading -->
               <div class="content">
                  <p class="small_bold new">Question Type</p>
                  <asp:DropDownList runat="server"  ID="QuestnType" CssClass="question_dropdown">
                  <asp:ListItem Value="1">Check Boxes (Multiple Choice)</asp:ListItem>
                  <asp:ListItem Value="2">Drop Down</asp:ListItem>
                  <asp:ListItem Value="3">Open Ended</asp:ListItem>
                  <asp:ListItem Value="4">Radio Buttons (Single Choice)</asp:ListItem>
                  <asp:ListItem Value="5">Range (Percentage)</asp:ListItem>
                  </asp:DropDownList>
                  <asp:DataList ID="nestedDataList" runat="server">
                     <ItemTemplate>
                         <p class="new">Answer <asp:Label ID="lblAnswerOrder" runat="server" Text='<%# Container.ItemIndex  + 1 %>'></asp:Label></p>
                         <asp:HiddenField ID="hiddenAnswerID" runat="server" Value='<%# Eval("AnswerID") %>' />
                         <asp:TextBox ID="TextBox1" runat="server" CssClass="form" Text='<%# Eval("AnswerID") %>' Width="300px"></asp:TextBox>
                         <asp:TextBox ID="tbAnswerText" runat="server" CssClass="form" Text='<%# Eval("AnswerTitle") %>' Width="300px"></asp:TextBox>
                     </ItemTemplate>
                 </asp:DataList>
                 <asp:Button runat="server" ID="updateName" CssClass="button_update" style="border: 0px;" onClick="UpdateQuestionName_Click" />
                 <asp:Button runat="server" ID="btnDelete" Text="Delete" OnClientClick="return confirm('Are you sure you want to delete this question?');" />
             </div>
         </div> <!-- end Questions -->
      </div> <!-- end questionbox -->
      <script type="text/javascript">
          jQuery(document).ready(function () {
              jQuery(".content").hide();
          });
      </script>
      <script type="text/javascript">
          jQuery(document).ready(function () {
              jQuery('#man').live('click', function (event) {
                  jQuery('.content').toggle('show');
              });
          });
      </script>   
  </ItemTemplate> 

最佳答案

试试这个:

jQuery(document).ready(function () {
        jQuery('#man').live('click', function (event) {
           $(this).closest('.heading').next().toggle('show');
        });
    });

关于javascript - 单击按钮时使用 JQuery 显示/隐藏某些 Div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9862425/

相关文章:

javascript - 使用 name 属性为 div 添加颜色

jquery 在元素之间包裹内容

asp.net - WiX(Votive)项目引用收获(HEAT)一个 ASP.Net Web 应用程序

c# - 在 ASP.NET MVC 中管理用户的最佳方式是什么

c# - __doPostBack 未作为控件的一部分包含在内

javascript - 如何使用 JavaScript 获取 HTML 中 <div> 中包含的 Paragraph 元素的值

javascript - Phonegap 是否支持 WebRTC?

javascript - 在 JavaScript/jQuery 中连接字符串?

javascript - Kendo UI 网格绑定(bind)错误

javascript - visual studio 11 javascript metro 应用程序部署错误