简体   繁体   English

如何根据用户选择动态更改多个复选框的编号?

[英]How to change the numbering of multiple check-boxes dynamically depending upon user selection?

I have 10 check-boxes. 我有10个复选框。 When user selects the first check-box then a label appears "a)." 当用户选择第一个复选框时,标签将显示为“ a)”。 beside it. 在它旁边。 On the selection of a second check-box, this label changes to "b)." 选择第二个复选框后,此标签将更改为“ b)”。 if it is placed lower in the ordering of the check-box's compared to the new selection, and the new check-box gets the labeling "a)." (如果它在复选框的顺序中比新选择的顺序低),则新复选框的标签为“ a)”。 and vice versa. 反之亦然。

I have found a way to do this by using binary approach, where I check if 1st, 2nd is selected then 3rd is "c)." 我找到了一种使用二进制方法执行此操作的方法,在该方法中,我检查是否选择了1st,2nd,然后选择3rd是“ c)”。 if not then "b)." 如果不是,则为“ b)”。 or "a)." 或“ a)”。 but this logic becomes too large and inefficient when a large no. 但是,如果没有,则逻辑变得太大而效率低下。 of check-boxes come in. 复选框进入。

Can some one help me find a better logic? 有人可以帮助我找到更好的逻辑吗?

Here is a sample code 这是示例代码

var a= optionA.header.CheckBox4.rawValue;
 var b= optionB.header.CheckBox4.rawValue;
 var c= optionC.header.CheckBox4.rawValue;
 var d= optionD.header.CheckBox4.rawValue; 
 if(this.rawValue == 1)
 {
    if(a==0 && b==0 && c==0 && d==0)
      optionE.number = "c)";
    else if(a==0 && b==0 && c==0 && d==1)
      optionE.number = "d)";
    else if(a==0 && b==0 && c==1 && d==0)
      optionE.number = "d)";    
    else if(a==0 && b==1 && c==0 && d==0)
      optionE.number = "d)";  
    else if(a==1 && b==0 && c==0 && d==0)
      optionE.number = "d)";  
    else if(a==1 && b==1 && c==0 && d==0)
      optionE.number = "e)";
    else if(a==1 && b==0 && c==1 && d==0)
      optionE.number = "e)";    
    else if(a==1 && b==0 && c==0 && d==1)
      optionE.number = "e)";  
    else if(a==0 && b==1 && c==0 && d==1)
      optionE.number = "e)";  
    else if(a==0 && b==1 && c==1 && d==0)
      optionE.number = "e)";  
    else if(a==0 && b==0 && c==1 && d==1)
      optionE.number = "e)";    
    else if(a==1 && b==1 && c==1 && d==0)
      optionE.number = "f)";    
    else if(a==1 && b==0 && c==1 && d==1)
      optionE.number = "f)";    
    else if(a==1 && b==1 && c==0 && d==1)
      optionE.number = "f)";    
    else if(a==0 && b==1 && c==1 && d==1)
      optionE.number = "f)";    
    else if(a==1 && b==1 && c==1 && d==1)
      optionE.number = "g)";    
 }
 else
 {
    optionE.number = "";    
 } 

The algorithmus of your code can be replaced by: 您的代码的算法可以替换为:

switch(a+b+c+d) {
case 0: optionE.number = "c)"; break;
case 1: optionE.number = "d)"; break;
case 2: optionE.number = "e)"; break;
case 3: optionE.number = "f)"; break;
case 4: optionE.number = "g)"; break;
}

I'm not sure what you really meant, as I understood, newest checked checkbox is 'a', next newest is 'b', .... 我不确定您真正的意思,据我了解,最新选中的复选框是“ a”,下一个最新复选框是“ b”,...。

If my understanding is correct, you may try this: 如果我的理解是正确的,则可以尝试以下操作:

 var cbArr = [], charArr = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']; $(':checkbox').change(function() { var $this = $(this); if ($this.is(':checked')) { // If checked, add element to end of array cbArr.push(this); } else { cbArr.splice(cbArr.indexOf(this), 1) // If not checked, remove element } $(':checkbox ~ label').text(''); $.each(cbArr, function(i, v) { var charIdx = cbArr.length - i - 1; // Get letter index $(v).next('label').text(charArr[charIdx]); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="checkbox" id="cb-1"> <label for="cb-1"></label> <br> <input type="checkbox" id="cb-2"> <label for="cb-2"></label> <br> <input type="checkbox" id="cb-3"> <label for="cb-3"></label> <br> <input type="checkbox" id="cb-4"> <label for="cb-4"></label> <br> <input type="checkbox" id="cb-5"> <label for="cb-5"></label> <br> <input type="checkbox" id="cb-6"> <label for="cb-6"></label> <br> <input type="checkbox" id="cb-7"> <label for="cb-7"></label> <br> <input type="checkbox" id="cb-8"> <label for="cb-8"></label> <br> <input type="checkbox" id="cb-9"> <label for="cb-9"></label> <br> <input type="checkbox" id="cb-10"> <label for="cb-10"></label> <br> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM