Javascript Jquery隐藏每个特定类
我试图根据第一次更改隐藏选项,但我尝试的代码不起作用,并且似乎没有给出任何错误 例如,如果用户选择A,则必须隐藏D 这是小提琴:Javascript Jquery隐藏每个特定类,javascript,jquery,Javascript,Jquery,我试图根据第一次更改隐藏选项,但我尝试的代码不起作用,并且似乎没有给出任何错误 例如,如果用户选择A,则必须隐藏D 这是小提琴: A. B C D E F $(“#mo”)。关于(“更改”,函数(){ var exclude=$(“选项:已选“,$(此)).attr(“类”); $(排除)。每个(函数(){ if($(this.val()!=$(“#mo选项:选中”).val()) $(this.hide(); }); }); 排除是一个类属性字符串,不能轻松用作选择器。我想你更想 $("#m
A.
B
C
D
E
F
$(“#mo”)。关于(“更改”,函数(){
var exclude=$(“选项:已选“,$(此)).attr(“类”);
$(排除)。每个(函数(){
if($(this.val()!=$(“#mo选项:选中”).val())
$(this.hide();
});
});
排除
是一个类属性字符串,不能轻松用作选择器。我想你更想
$("#mo").on("change", function(){
var selected = $("option:selected", this),
selectedClass = selected.attr("class");
$("option", this).not(selected).filter(function() {
return $(this).attr("class") == selectedClass;
}).hide();
});
使用Jquery选择器时,缺少“.”部分 ex$('.'不包括在内) 另外,因为exclude将包含多个类,所以您可能希望将它们拆分为一个数组,每个数组中各有一个点。或者可能有更好的方法来做这件事。Html: Demoi: 您在这里做什么,$(“选项:已选定,$(此)).attr(“类”)@jp310:
$("#mo").on("change", function(){
var selected = $("option:selected", this),
selectedClass = selected.attr("class");
$("option", this).not(selected).filter(function() {
return $(this).attr("class") == selectedClass;
}).hide();
});
<select id="mo" name="mo">
<option value=""></option>
<option class="m t t2" value="A">A</option>
<option class="m t" value="B">B</option>
<option class="m t" value="C">C</option>
<option class="m t t2" value="D">D</option>
<option class="f t t2" value="E">E</option>
<option class="f t" value="F">F</option>
</select>
$(document).on('change','#mo',function(){
var exclude = $("option:selected", $(this)).attr("class");
$('#mo option').each(function() {
if($(this).attr("class") != exclude)
$(this).hide();
});
});