小编Dip*_*pak的帖子

jquery父子选择器返回undefined

为什么jquery父子选择器在这里不起作用.

这里的article元素有它的子元素部分,而section包含html select标签.

所以,有了父子逻辑,它必须工作,不是吗?它返回undefined.

$(document).on('change', 'select[name="slct_sec"]', function(){

    //This one is working
    var cls=$('section > select[name="slct_cls"]').val();
    
    //Not working
    var cls_1=$(this).parent('section').siblings('section > select[name="slct_cls"]').val();
 
   //Not working
    var cls_2=$(this).parent('article').children('section > select[name="slct_cls"]').val();
    alert(cls_1);
    alert(cls_2);
	
  });
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
  <head>
  </head>
  <body>
    <article>
      <section>
        <select name='slct_cls'>
          <option value='1'>One</option>
          <option value='2'>Two</option>
        </select>
      </section>
      <br/>
      <section>
        <select name='slct_sec'>
          <option value='1'>A</option>
          <option value='2'>B</option>
        </select>
      </section>
    </article>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

javascript jquery

2
推荐指数
1
解决办法
164
查看次数

如何只显示当前李的孩子并隐藏其他李子

我的页面中有多个嵌套的ul.嵌套的ul通过以下jquery代码显示.

jQuery的

$("li").click(function(){
    $(this).children("ul").show("slow");
});
Run Code Online (Sandbox Code Playgroud)

但是我想一次只显示一个嵌套的ul.我的意思是当我点击一个li时,它应该只显示其子ul并隐藏其他孩子ul.我在之前的jquery代码中添加了以下行

$("li ul").not(this).hide("slow");
Run Code Online (Sandbox Code Playgroud)

但它不起作用.

CSS

li ul{
    display:none;
}
Run Code Online (Sandbox Code Playgroud)

HTML

<li>
    <a href="#">Insert + </a>
    <ul>
        <li>
            <a href="services.php">Services</a>
        </li>
        <li>
            <a href="notice.php">Notice and Information</a>
        </li>
    </ul>
</li>

<li>
    <a href="#">View + </a>
    <ul>
        <li>
            <a href="services.php">Comments</a>
        </li>
        <li>
            <a href="notice.php">Status</a>
        </li>
    </ul>
</li>
Run Code Online (Sandbox Code Playgroud)

jquery parent-child jquery-selectors

0
推荐指数
1
解决办法
1375
查看次数