• 根据checkBox或radio的勾选状态得到id数组


     1 $(function(){
     2         var inputs = document.getElementsByTagName("input");
     3         var realStrs = "";
     4         var strs = "";
     5         for (var i = 0; i < inputs.length; i++) {
     6             var obj = inputs[i];
     7             if (obj.type == "radio") {
     8                 if (obj.checked == true) {
     9                     realStrs += obj.id + ",";
    10                 }
    11             }else if(obj.type == "checkbox"){
    12                 if(obj.checked == true){
    13                     strs += obj.id + ",";
    14                 }
    15             }
    16         }
    17         realStrs = realStrs.substr(0,realStrs.length-1);
    18         strs = strs.substr(0,strs.length-1);
    19         var singleArray = realStrs.split(",");
    20         var array = strs.split(",");
    21         var result = [], isRepeated;
    22         for (var i = 0, len = singleArray.length; i < len; i++) {
    23             isRepeated = false;
    24             for (var j = i+1, len = singleArray.length; j < len; j++) {
    25                 if (singleArray[i] == singleArray[j]) {   
    26                     isRepeated = true;
    27                     break;
    28                 }
    29             }
    30             if (!isRepeated) {
    31                 result.push(singleArray[i]);
    32             }
    33         }
    34         var result2 = [], isRepeated2;
    35         for (var i = 0, len = array.length; i < len; i++) {
    36             isRepeated2 = false;
    37             for (var j = i+1, len = array.length; j < len; j++) {
    38                 if (array[i] == array[j]) {   
    39                     isRepeated2 = true;
    40                     break;
    41                 }
    42             }
    43             if (!isRepeated) {
    44                 result2.push(array[i]);
    45             }
    46         }
    47         for (var k = 0; k < result.length; k++) {
    48             var realIds = result[k] - 99;
    49             if(realIds>0){                
    50                 document.getElementById(realIds).style.display = "none";
    51             }
    52         }
    53         for (var j = 0; j < result2.length; j++) {
    54             var ids = result2[j] - 100;
    55             if(ids>0){
    56                 document.getElementById(ids).style.display = "none";                
    57             }
    58         }
    59     });
    View Code

       这段代码是在查看问卷调查结果的时候用到的,一份问卷里面有单选题、多选题和问答题,(我这里不考虑最简单的问答题),

    我的方法是:所有选项前面都有一个input,单选就是radio,多选就是checkbox,将查询到的结果进行判断,已选的选项前再加上

    一个input,属性checked=“checked”,并将刚刚的input隐藏。

    for (var k = 0; k < result.length; k++) {

      var realIds = result[k] - 99;
      if(realIds>0){                
          document.getElementById(realIds).style.display = "none";
      }
    } for (var j = 0; j < result2.length; j++) { var ids = result2[j] - 100; if(ids>0){ document.getElementById(ids).style.display = "none"; } }
    其中result[k]-99,表示的是选中的选项是有两个input的,只是将其中一个没选中的隐藏了。
    ————————————————————————————————————————————————————————————————————————————————————————————
      大家要看的只是我得到checked的属性的id就可以了,其他的可作为参考。
  • 相关阅读:
    155. 最小栈
    160. 相交链表
    PAT 1057 Stack
    PAT 1026 Table Tennis
    PAT 1017 Queueing at Bank
    PAT 1014 Waiting in Line
    PAT 1029 Median
    PAT 1016 Phone Bills
    PAT 1010 Radix
    PAT 1122 Hamiltonian Cycle
  • 原文地址:https://www.cnblogs.com/huskyking/p/5876840.html
Copyright © 2020-2023  润新知