如何在单击div时获取Div id

时间:2013-06-13 04:27:14

标签: javascript jquery html

   <div id=ControlId>
<span>Select Option</span> 
</div>
  </div>

   <div id=ControlId + "_child"  style="display: none" > 
<table>
    <tr>
        <td valign="top" style="width: 165px" ><input type="checkbox" name="vehicle" value="Bike" /> option 1</td>
    </tr>
    <tr>
        <td valign="top" style="width: 165px" ><input type="checkbox" name="vehicle" value="Car" /> option 2</td>
    </tr>
    <tr>
        <td valign="top" style="width: 165px" ><input type="checkbox" name="vehicle" value="dog" /> option 3</td>
    </tr>
</table>
</div>

我已经在td中添加了多次,如下图

enter image description here

如何在点击事件中实现主div的id,实际上我的问题是如果我点击第一个div,最后只添加了打开

$("#" + ControlId).click(function () {
    $("#" + ControlId + "_child").fadeIn("slow");
    $("#" + ControlId + "_child").toggle();
});

这是我的实际事件代码

3 个答案:

答案 0 :(得分:1)

试试这个 HTML

    <div id="select1" data="select">
    <span>Select Option</span>
</div>
<div toggle="select1" style="display: none">
    <table>
        <tr>
            <td valign="top" style="width: 165px">
                <input type="checkbox" name="vehicle" value="Bike" />
                option 1
            </td>
        </tr>
        <tr>
            <td valign="top" style="width: 165px">
                <input type="checkbox" name="vehicle" value="Car" />
                option 2
            </td>
        </tr>
        <tr>
            <td valign="top" style="width: 165px">
                <input type="checkbox" name="vehicle" value="dog" />
                option 3
            </td>
        </tr>
    </table>
</div>
<div id="select2" data="select">
    <span>Select Option</span>
</div>
<div toggle="select2" style="display: none">
    <table>
        <tr>
            <td valign="top" style="width: 165px">
                <input type="checkbox" name="vehicle" value="Bike" />
                option 1
            </td>
        </tr>
        <tr>
            <td valign="top" style="width: 165px">
                <input type="checkbox" name="vehicle" value="Car" />
                option 2
            </td>
        </tr>
        <tr>
            <td valign="top" style="width: 165px">
                <input type="checkbox" name="vehicle" value="dog" />
                option 3
            </td>
        </tr>
    </table>
</div>

的javascript

<script type="text/javascript">
    $(document).ready(function () {
        $('[data="select"]').click(function () {
            var selectdiv = $(this);
            $('[toggle]:not([toggle="' + selectdiv.attr('id') + '"])').hide();
            $('[toggle="' + selectdiv.attr('id') + '"]').toggle(); ;
        })
    })
</script>

答案 1 :(得分:0)

为什么不

$("#" + ControlId).click(function () {
    $("#" + this.id + "_child").fadeIn("slow");
    $("#" + this.id + "_child").toggle();
});

答案 2 :(得分:-1)

而不是id为div提供类

     <div id="ControlId" class="ControlId">
      <span>Select Option</span> 
      </div>
    </div>


 $(function()
 {

$(".ControlId").click(function()
 {
 var div=$(this);
 //hide or toggle child divs

 });
});