【发布时间】:2016-12-23 00:41:39
【问题描述】:
我有一个包含三个选择选项的表单:
- 适合
- 颜色
- 尺寸
默认情况下,“适合”下拉菜单和“颜色”下拉菜单处于活动状态,并选择了默认值(例如,常规适合和蓝色)。
共有三种不同的“尺寸”下拉菜单,但根据从“适合”下拉菜单中选择的内容,在任何时候只有一个可见。
如果选项 value="none",则该按钮被禁用。
问题
仅当所有三个“尺寸”下拉菜单都被更改以使其值不是“无”时,按钮才会变为活动状态(这是通过为常规选择初始尺寸,然后从“适合”下拉菜单中选择小和长来完成的)。理想情况下,我只希望按钮考虑处于活动状态的“尺寸”下拉菜单。
更新
@nagappan 下面提供的工作 jsFiddle 解决方案,非常感谢。
https://jsfiddle.net/dodgers76/c0dvdwbz/
var currentSelectedVals = {'selector-fit':'','selector-color':'','selector-sizes':''};
var disableComboVals = [
{'selector-fit':'','selector-color':'','selector-sizes':'none'},
{'selector-fit':'petite','selector-color':'','selector-sizes':'10'},
{'selector-fit':'petite','selector-color':'','selector-sizes':'20'},
{'selector-fit':'petite','selector-color':'','selector-sizes':'22'},
{'selector-fit':'petite','selector-color':'','selector-sizes':'24'},
{'selector-fit':'long','selector-color':'','selector-sizes':'22'},
{'selector-fit':'long','selector-color':'','selector-sizes':'24'}
];
function checkDisableCombo() {
return $.grep(disableComboVals, function(vals){
cnt = 0;
$.each(vals, function(key,val) {
console.log('comparing key val '+key+val);
if (val === '' || val === currentSelectedVals[key]) {
console.log('>>matched values');
cnt = cnt + 1;
}
});
if (cnt===3) {
return true;
}
return false;
});
};
$(function(){
var sizeVal = 'none';
$("select.selector-fit").on("change", function(){
//remove active
$("select.selector-sizes.active").removeClass("active");
//check if select class exists. If it does then show it
var subList = $("select.selector-sizes."+$(this).val());
if (subList.length){
//class exists. Show it by adding active class to it
subList.addClass("active");
subList.val(sizeVal);
}
});
$('.selector-sizes').on('change', function() {
sizeVal = $(this).val();
});
});
$(function() {
$('.selector').on('change', function() {
var $sels = $('option.selector-sizes:selected[value="none"]');
var isSizeSelector = jQuery.inArray( "selector-sizes",this.classList);
currentSelectedVals[this.classList[1]] = this.value;
console.log(currentSelectedVals);
var result = checkDisableCombo();
console.log(result);
if ( result.length > 0) {
console.log('disabled false');
$("#Testing").attr("disabled", true);
} else {
$("#Testing").attr("disabled", false);
}
}).change();
});
【问题讨论】:
-
您无法通过这种方式获取对象:
$('.selector option:selected[value="none"]')选择器错误。
标签: javascript jquery html css