Javascript 过滤选择下拉框选项
我试图过滤一些下拉选择框,但在其中一个选项中出现了一些奇怪的行为 这是我正在使用的jQuery:Javascript 过滤选择下拉框选项,javascript,jquery,html,Javascript,Jquery,Html,我试图过滤一些下拉选择框,但在其中一个选项中出现了一些奇怪的行为 这是我正在使用的jQuery: $(document).ready(function () { $('select:gt(0)').find('option:gt(0)').hide().prop('disabled', true); $('#C5R').change(function () { $('select:gt(0)').find('option:gt(0)').hide(); var thisVal
$(document).ready(function () {
$('select:gt(0)').find('option:gt(0)').hide().prop('disabled', true);
$('#C5R').change(function () {
$('select:gt(0)').find('option:gt(0)').hide();
var thisVal = $(this).val();
if (thisVal == 1) {
$('#C5T').find('option').each(function () {
var thisVal = $(this).val();
$(this).hide().prop('disabled', true);;
if ((thisVal >= 1) && (thisVal <= 11)) {
$(this).show().prop('disabled', false);;
}
});
} else if (thisVal == 2) {
$('#C5T').find('option').each(function () {
var thisVal = $(this).val();
$(this).hide().prop('disabled', true);;
if ((thisVal >= 12) && (thisVal <= 32)) {
$(this).show().prop('disabled', false);;
}
});
} else if (thisVal == 3) {
$('#C5T').find('option').each(function () {
var thisVal = $(this).val();
$(this).hide().prop('disabled', true);;
if ((thisVal >= 33) && (thisVal <= 51)) {
$(this).show().prop('disabled', false);;
}
});
}
});
});
$(文档).ready(函数(){
$('select:gt(0)').find('option:gt(0)').hide().prop('disabled',true);
$('#C5R')。更改(函数(){
$('select:gt(0)').find('option:gt(0)').hide();
var thisVal=$(this.val();
如果(thisVal==1){
$('#C5T')。查找('option')。每个(函数(){
var thisVal=$(this.val();
$(this.hide().prop('disabled',true);;
如果((thisVal>=1)&&(thisVal=12)&(thisVal=33)&(thisVal一种方法是使用隐藏的选择具有所有选项的,您只需在更改时复制所需的选项
HTML
<select name="C5T" size="1" id="C5T" onchange="{C5TSelectChange(); onBlurUpdate(this)}" class="fontSize"></select>
<select id="C5Thidden" style="display:none">
<option class="fontSize"></option>
<option value="1" class="fontSize">Dereham</option>
<option value="2" class="fontSize">East Dereham</option>
...
<option value="51" class="fontSize">Witham</option>
</select>
JSFIDLE实际上对我来说很好(OSX上的Chrome)。你使用的Chrome和操作系统是什么版本?Chrome 31.0.1650.63 m和win 7I刚刚意识到你试图隐藏选择选项,据我所知这是不可能的(至少在Chrome中是不可能的)。有关更多信息,请参阅隐藏在Chrome中起作用,只是选择Essex时的显示问题。在IE中尝试了它,它会显示所有选项,并根据选择的选项禁用它们,因此猜测隐藏在IE中根本不起作用,但仍能正常工作。隐藏在Chrome中实际上对我不起作用,我不知道它为什么对我起作用不过,在某些情况下,如果我在谷歌上快速搜索这个问题,它似乎真的不可靠。我不会使用它,我在之前的评论中添加的链接描述了一个简单的jQuery插件,它可以临时删除一个选项,稍后使用数据功能将其放回。
$(document).ready(function () {
$('#C5R').change(function () {
var thisVal = $(this).val();
$C5T = $('#C5T');
$C5T.empty();
$options = $('option', '#C5Thidden');
if (thisVal == 1) {
filteredOptions = $options.slice(1, 12).clone();
$C5T.append(filteredOptions);
} else if (thisVal == 2) {
filteredOptions = $options.slice(12, 33).clone();
$C5T.append(filteredOptions);
} else if (thisVal == 3) {
filteredOptions = $options.slice(33).clone();
$C5T.append(filteredOptions);
}
});
});