jQuery的:删除基于属性阵列和放大器的选择选项;其他选择框选择 [英] jquery: remove select options based on attribute array & other select box selection
问题描述
我试图建立一套的选择框,以相当大的数据集快速过滤成可用块。我非常接近,但接受我想要解决的正好相反,我是否使用。不()选择(或取出来),或者设置使用.attr =通话(或.attr!= )。
I'm trying to set up a set of select boxes to quickly filter fairly large datasets into usable chunks. And I'm very close, but receiving the exact opposite of the solution I want, whether or not I use .not() selector (or take it out), or set up the call using .attr= (or .attr!=).
下面是一个小提琴所以你可以看到发生了什么:
Here's a fiddle so you can see what's going on:
http://jsfiddle.net/yD5cG/3/
(忽视的事实是,一旦你改变了顶级的选择,底部选择框不会改变。我不需要担心这一点)。
http://jsfiddle.net/yD5cG/3/ (Ignore the fact that once you change the top-level selection, the bottom select box doesn't change. I won't need to worry about this).
我搜罗了现场,发现有很多code的这是接近我需要什么,但我想解析的数组抛出它关掉?在这一点上,如果我是过于复杂的事物或囫囵吞枣我不能告诉。 (这基本上是在使用JQuery简单许多一对多过滤器的尝试)。
I've trawled the site and found a lot of code that's close to what I need, but I think parsing arrays is throwing it off? At this point, I can't tell if I'm over-complicating things or over-simplifying. (This is basically an attempt at a simple many-to-many filter using JQuery).
感谢您的任何东西,甚至连最基本的想法......
Thanks for anything, even the most basic idea...
推荐答案
证明: http://jsfiddle.net / iambriansreed / KBKEV /
var options = $('#selectbox2').html();
//store original options
$("#selectbox1").change(function() {
var selected = this.value.split(',');
// get selected value and make it an array
$('#selectbox2').html(options);
// reset the box to the original options
$('#selectbox2 option').filter(function(){
if(this.value.indexOf(',') == -1){
// simple single values
return $.inArray(this.value, selected) == -1;
}else{
// check each value
var values = this.value.split(',');
for(i in values){
if($.inArray(values[i], selected) > -1)
return false;
}
return true;
}
}).remove();
// remove any options not in the array of selected values
});
一个工作小提琴,一步解释了一步。另一种质量的答案。
A working fiddle and a step by step explanation. Another quality answer.
这篇关于jQuery的:删除基于属性阵列和放大器的选择选项;其他选择框选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!