Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 仅选择具有类名的选项_Jquery - Fatal编程技术网

Jquery 仅选择具有类名的选项

Jquery 仅选择具有类名的选项,jquery,Jquery,默认情况下,所有选项都处于选中状态。然后从返回类名zzz的click事件中,我只想用class='zzz'显示选项的其余部分 $document.on'click',.test,function{ var selectedParent=$this.val; alertselectedParent;//返回zzz $'users>option'.eachfunction{ 如果$this.hasClass选择了父级{ $this.show; }否则{ $this.hide; }; }; 用户1

默认情况下,所有选项都处于选中状态。然后从返回类名zzz的click事件中,我只想用class='zzz'显示选项的其余部分

$document.on'click',.test,function{ var selectedParent=$this.val; alertselectedParent;//返回zzz $'users>option'.eachfunction{ 如果$this.hasClass选择了父级{ $this.show; }否则{ $this.hide; }; }; 用户1 用户2 用户3 用户4
问题是您引用了selectedParent,因此它没有使用变量值

此外,您可以将类替换为选择器,而不是使用.each

$document.on'click',.test,function{ var selectedParent=$this.val; $`users>选项。${selectedParent}`.show; $`users>选项:不。${selectedParent}`.hide; }; 用户1 用户2 用户3 用户4 ZZZ AAA像一个选项:

$document.on'click',users>选项,函数{ var selectedParent=$this.attr'class'; $'users>option'.eachfunction{ 如果!$this.hassclassselectedparent{ $this.hide; } }; }; 用户1 用户2 用户3 用户4
在HTML中class=test在哪里?去掉selectedParent的引号以使用变量的值。否则,您将查找文本类selectedParent。正如@Barmar所说,代码中没有名为test的类?