【问题标题】:Show/Hide Certain Div with JQuery on Button Click在按钮单击时使用 JQuery 显示/隐藏某些 Div
【发布时间】:2012-03-25 17:40:10
【问题描述】:

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

我可以正确显示或隐藏 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> 

【问题讨论】:

  • 一个 id 属性值只能在一个页面上使用一次。

标签: javascript jquery asp.net button toggle


【解决方案1】:

试试这个:

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

【讨论】:

  • 这会导致 div 反弹。如果div被隐藏并且我按下按钮是:打开->关闭->打开。然后保持可见/打开。如果再次按下按钮,则顺序相反。有什么想法吗?
  • 已解决:我从 ASP 数据列表的项目模板中删除了脚本。
【解决方案2】:

如果您将 id 属性替换为类值,则以下代码更改可能会起作用:

jQuery('.man').live('click', function (event) {
    jQuery(this).parents().find(jQuery('.content')).toggle('show');
});

【讨论】:

  • 对此的一般说明:如另一条评论中所述,您不应在页面上多次使用相同的 id 值,因为它是唯一标识符。如果更多 DOM 对象具有相同的标识符,则某些浏览器可能难以识别正确的 DOM 对象。
【解决方案3】:

设置 ".content" css display:none 并写下这个。

<script type="text/javascript">
    $(document).ready(function () {
        jQuery('#man').toggle(function () {
            jQuery(".content").sliceDown();
        }, function () {
            jQuery(".content").sliceUp();
        });
    });
</script>

【讨论】:

    猜你喜欢
    • 2016-01-20
    • 1970-01-01
    • 2013-01-16
    • 2016-05-21
    • 1970-01-01
    • 1970-01-01
    • 2021-06-14
    • 1970-01-01
    • 2013-04-24
    相关资源
    最近更新 更多