2012-03-25 17 views
2

ボタンをクリックするとdivを表示/非表示にしようとしています。私はASP.NETを使用しており、すべてがASP:Datalistの中にあります。ボタンでJQueryを使用して特定のDivを表示/非表示

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> 

+1

id属性値はページ上で1回だけ使用する必要があります。 – Daxcode

答えて

2

はこれを試してみてください。

​​
+0

これによりdivがバウンスします。 divを非表示にしてボタンを押すと、開く - >終了 - >開くと表示されます。その後、可視/開いたままになります。ボタンをもう一度押すと、逆順になります。何か案は? – HGomez90

+0

解決済み:ASPデータリストのアイテムテンプレートからスクリプトを削除しました。 – HGomez90

1

あなたは、コードの変更がうまくいくかもしれない下のクラス値を持つid属性、置き換えた場合:

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

その一般的な注記:他のコメントで述べたように、一意の識別子なので、ページ上で同じid値を複数回使用しないでください。いくつかのブラウザは、より多くのDOMオブジェクトが同じ識別子を持つ場合、正しいDOMオブジェクトを識別するために苦労する可能性があります。 – Daxcode

+0

あなたが指摘し、IDをクラスに変更しました。 – HGomez90

0

セット ".content" CSS表示:なしに、これを書きます。

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