2014-10-17 60 views
1

让我们假设我们具有A,B,C和D值的参数。现在我们要强制用户只选择A,B,C或A,C,D或A或B或C.允许仅从可用值中选择predifened组合

而不是允许所有可能的16个组合,我们只允许5个预定义的组合。我尝试过,但为此,我必须为每一个选择提供条件。

如果我们假设这个值与复选框绑定,并且我们需要检查选择的值是否与我们的预先组合一样。

我需要在javascript或angular.js中实现这一点。请帮助我使用适当的算法进行此类操作。

我尝试下面的逻辑来实现这一点,但只有在最后提交这不会Infor的用户立即,警报

// 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'); 
    } 
} 

此代码只给出了关于联合信息有效与否,哪些是可能的组合根据选择。

+0

你可以发布你到目前为止的代码吗? – 2014-10-17 05:43:10

+0

@NicolásStraubValdivieso我已经添加了我的逻辑虚拟样本。 – user3725294 2014-10-17 06:10:06

+1

“最终提交后才提醒”是什么意思?另外你在那里输入的最后一段是什么意思? (你想告诉用户所有有效的组合?) – 2014-10-17 06:16:30

回答

1

https://stackoverflow.com/a/1885660/1029988被盗:

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'); 
    } 
} 

当然你会想来格式化输出字符串,但该代码将(希望,它毕竟是餐巾纸代码)给你缺失的位,使你已经选择已有的组合有效的组合

+1

不客气:) – 2014-10-17 06:44:35

+0

没有它按预期工作:(..对于选定的组合['a','b','d']它不告诉用户选择'c'来代替'如果一个有效的梳子将是['a','b','c']或'c'来代替'b',所以一个有效的梳子将会是['a','c','d' '] – user3725294 2014-10-17 06:50:31

+1

是的,这段代码希望活动组合是一个有效组合的子集......如果它不是早上4点,那么我会为你写另一个循环......基本上用于所有项目'preDefinedCombinations'其中交点小于选定值,您需要遍历'selectedvalues'数组找到所有位于'selectedvalues'但不在'preDefinedCombinations [i]'中的位(称之为'inverse_missing_bits'),然后从'selectedvalues'中去掉这些值并运行'j'循环找到缺失的位。(在下一条评论中继续) – 2014-10-17 06:59:05

1

可能是下面的代码可以帮助您解决乌尔问题

<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>