Javascript e:)。我更新了代码,它应该为这个问题提供一个完整的解决方案。但它仍然是餐巾纸代码,因此如果它不起作用,请让我知道,以便我们可以一起修复好,我测试了它,发现['a','b','d']警报无效组合,如果您选择其中任何一个,您将获得有效组合:或者,您可以通过取
Javascript e:)。我更新了代码,它应该为这个问题提供一个完整的解决方案。但它仍然是餐巾纸代码,因此如果它不起作用,请让我知道,以便我们可以一起修复好,我测试了它,发现['a','b','d']警报无效组合,如果您选择其中任何一个,您将获得有效组合:或者,您可以通过取,javascript,angularjs,Javascript,Angularjs,e:)。我更新了代码,它应该为这个问题提供一个完整的解决方案。但它仍然是餐巾纸代码,因此如果它不起作用,请让我知道,以便我们可以一起修复好,我测试了它,发现['a','b','d']警报无效组合,如果您选择其中任何一个,您将获得有效组合:或者,您可以通过取消选择某些位并选择其他位来达到有效组合:取消选择:d,选择:c;取消选择:b,选择:c;取消选择:b,d,选择:取消选择:a,d,选择:;取消选择:a、b、d、选择:c` // multi-dimentional array of define
e:)。我更新了代码,它应该为这个问题提供一个完整的解决方案。但它仍然是餐巾纸代码,因此如果它不起作用,请让我知道,以便我们可以一起修复好,我测试了它,发现
['a','b','d']
警报无效组合,如果您选择其中任何一个,您将获得有效组合:或者,您可以通过取消选择某些位并选择其他位来达到有效组合:取消选择:d,选择:c;取消选择:b,选择:c;取消选择:b,d,选择:取消选择:a,d,选择:;取消选择:a、b、d、选择:c`
// multi-dimentional array of defined combinations
var preDefinedCombinations = [['a','b','c'], ['a','c','d'], ['a'], ['b'], ['c']];
// Combination user select
var selectedvalues = [];
// Function called on selection or removing value (i.e a,b,c,d)
function selectOption(value){
var checkIndex = selectedvalues.indexof(value);
if(checkIndex == -1){
selectedvalues.push(value);
}else{
selectedvalues.splice(checkIndex, 1);
}
}
// function called on submition of combination
function checkVaildCombination(){
if(preDefinedCombinations.indexOf(selectedvalues) == -1){
alert('Invalid Combination');
}else{
alert('Valid Combination');
}
}
<script>
function validateForm(){
var checkBoxValues = this.a.checked.toString() + this.b.checked.toString() + this.c.checked.toString() + this.d.checked.toString();
if( checkBoxValues == 'truetruetruefalse' || // abc
checkBoxValues == 'truefalsetruetrue' || // acd
checkBoxValues == 'truefalsefalsefalse' || // a
checkBoxValues == 'falsetruefalsefalse' || // b
checkBoxValues == 'falsefalsetruefalse' ){ // c
return true;
}
return false;
}
</script>
<form onsubmit="return validateForm()" action="javascript:alert('valid')">
<input type="checkbox" name="mygroup" id="a">
<input type="checkbox" name="mygroup" id="b">
<input type="checkbox" name="mygroup" id="c">
<input type="checkbox" name="mygroup" id="d">
<input type="submit">
</form>
function intersect_safe(a, b)
{
var ai=0, bi=0;
var result = new Array();
while( ai < a.length && bi < b.length )
{
if (a[ai] < b[bi] ){ ai++; }
else if (a[ai] > b[bi] ){ bi++; }
else /* they're equal */
{
result.push(a[ai]);
ai++;
bi++;
}
}
return result;
}
function checkVaildCombination(){
function get_diff(superset, subset) {
var diff = [];
for (var j = 0; j < superset.length; j++) {
if (subset.indexOf(superset[j]) == -1) { // actual missing bit
diff.push(superset[j]);
}
}
return diff;
}
if(preDefinedCombinations.indexOf(selectedvalues) == -1){
missing_bits = [];
diffed_bits = [];
for (var i = 0; i < preDefinedCombinations.length; i++) {
var intersection = intersect_safe(preDefinedCombinations[i], selectedvalues);
if (intersection.length == selectedvalues.length) { // candidate for valid answer
missing_bits.push(get_diff(preDefinedCombinations[i], intersection));
} else {
var excess_bits = get_diff(selectedvalues, intersection),
missing_bit = get_diff(preDefinedCombinations[i], intersection);
diffed_bits.push({
excess: excess_bits,
missing: missing_bit
});
}
}
var message = 'Invalid Combination, if you select any of these you`ll get a valid combination:\n\n' + missing_bits.toString();
message += '\n\n Alternatively, you can reach a valid combination by deselected some bits and select others:\n';
for (var j = 0; j < diffed_bits.length; j++) {
message += '\ndeselect: ' + diffed_bits[j].excess.toString() + ', select: ' + diffed_bits[j].missing.toString();
}
alert(message);
} else {
alert('Valid Combination');
}
}