如何跟踪复选框组中的组合更改

时间:2017-12-22 05:49:18

标签: jquery

您能否请看下面的代码段,让我知道如何跟踪组合检查或取消检查以获得以下结果

    If A Only  -----> phaseVal = "2,0"
    If B Only  -----> phaseVal = "3,0"
    If C Only  -----> phaseVal = "1,0"  
    If AB  ---------> phaseVal = "3,4,5,0" 
    If AC  ---------> phaseVal = "6,4,2,,0" 
    If BC  ---------> phaseVal = "1,0" 
    If ABC ---------> phaseVal = "7,6,5,4,3,0" 

var phaseVal ="";
$('input:checkbox[name=phase]').on('change', function() {
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" name="phase" value="" />A <br />
<input type="checkbox" name="phase" value="" />B<br />
<input type="checkbox" name="phase" value="" />C<br />

1 个答案:

答案 0 :(得分:1)

var phaseVal ="";
$('input:checkbox[name=phase]').on('change', function() {
var str = 'input:checkbox[name=phase]:checked[value="';
  if($(str+'A"]').length*$(str+'B"]').length) console.log('A and B selected');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" name="phase" value="A" />A <br />
<input type="checkbox" name="phase" value="B" />B<br />
<input type="checkbox" name="phase" value="C" />C<br />

您想将值更改为此字母