dpf7891 2017-12-26 02:23
浏览 84
已采纳

根据三个php变量限制数组中的可选复选框(每列最多1个,每行10个,总共15个)

I have an array of checkboxes generated with php. I can not display every checkbox because of previous choices by other users.

for($i=0;$i<$maxrows;$i++)
{
   $already_selected[$i]=0; 
   for($a=0;$a<$maxcols;$a++)
   {
      $value=$mat[$i][$a];
      if($value>0)
      {
         echo"<input type='checkbox' name='arr[$i][$a]' value='$uid'/>";
      }  
      else
      {
         echo"<input type='hidden' name='arr[$i][$a]' value='$value'/>";
         $already_selected[$i]+=1; 
      }
   }   
}

I have to limit the selectable checkboxes in a (for me, at least) complex way: The user must be able to choose: - One checkbox for each column maximum - Maximum $x checkboxes per row (where x is calculated by php as: $maximum_per_row - $already_selected) - Maximum $y checkboxes over the entire grid (where y is a php variable received by the previous page).

Is it possible?

Maybe something like:

echo"
      <script type='text/javascript'>
          var limit = $y;
          $('input.ggrid').on('change', function(evt)
          {
            if($('input[class='ggrid']:checked').length >= limit)
            {
              this.checked = false;
            }
          });
      </script>
";
for($i=0;$i<$maxrows;$i++)
{
   echo"
      <script type='text/javascript'>
          var limit = $maximum_per_row[$i] - $already_selected[$i];
          $('input.row$i').on('change', function(evt)
          {
            if($('input[class='row$i']:checked').length >= limit)
            {
              this.checked = false;
            }
          });
      </script>
   "; 
   for($a=0;$a<$maxcols;$a++)
   {
      echo"
      <script type='text/javascript'>
          var limit = 1;
          $('input.col$a').on('change', function(evt)
          {
            if($('input[class='col$a']:checked').length >= limit)
            {
              this.checked = false;
            }
          });
      </script>
      ";
   }   
}
for($i=0;$i<$maxrows;$i++)
{
   $already_selected=0; 
   for($a=0;$a<$maxcols;$a++)
   {
      $value=$mat[$i][$a];
      if($value>0)
      {
         echo"<input type='checkbox' class='col$a row$i ggrid'name='arr[$i][$a]' value='$uid'/>";
      }  
      else
      {
         echo"<input type='hidden' name='arr[$i][$a]' value='$value'/>";
         $already_selected+=1; 
      }
   }   
}

which obviously does not work... Thank you in advance!

  • 写回答

1条回答 默认 最新

  • dongle0396 2017-12-27 02:08
    关注

    Please refer to the following code. PHP and pure javascript, no jQuery.

    <?php
    
    $maxrows=4; //example, you could replace it with your value
    $maxcols=8; //example, you could replace it with your value
    $y=1;       //example, you could replace it with your value
    $x=array();
    
    for($i=0;$i<$maxrows;$i++)
    {
        $already_selected[$i]=0; 
        $test_symbol=1; //value to let half of the inputs are hidden for example, you could delete it
        for($a=0;$a<$maxcols;$a++)
        {
            //$value=$mat[$i][$a];
            $test_symbol=$test_symbol==1?-1:1;
            $value=$a*$test_symbol; //as example, you could replace it with your value
            $uid=$a; //as example
            if($value>0)
            {
                echo"<input type='checkbox' id='arr_".$i."_".$a."' name='arr_".$i."_".$a."' value='$uid'/ onclick='check_limit($i,$a)'> 
    ";
            }  
            else
            {
                echo"<input type='hidden' name='arr_".$i."_".$a."' value='$value'/> 
    ";
                $already_selected[$i]+=1; 
            }
        }
        echo "$i row selected:".$already_selected[$i]."
     <br>"; 
        $x[$i] = $maxcols-$already_selected[$i]-$y;
    
    }
    
    ?>
    
    <script language="javascript">
    var row_limit = <?php echo json_encode($x) ?>;
    
    var maxrows = <?php echo $maxrows ?>;
    var maxcols = <?php echo $maxcols ?>;
    
    function check_limit(row,col){
    
        //row control, max allowd by array calculated from PHP
        for(i = 0; i < maxrows; i++){
            tempSelected = 0;
    
            for(j=0;j<maxcols;j++){
                if(document.getElementById("arr_"+i+"_"+j)){
                    if(document.getElementById("arr_"+i+"_"+j).checked){
                        tempSelected++;
                    }
                }
            }
            if(tempSelected>row_limit[i]){
                alert("Maximum " + row_limit[i] + "per row allowed!");
                document.getElementById("arr_" + row + "_" + col).checked=false;
                return;
            }
        }
    
        //column control, max 1 allowed for each column
        for(j=0;j<maxcols;j++){
            tempSelected = 0;
    
            for(i=0;i<maxrows;i++){
                if(document.getElementById("arr_"+i+"_"+j)){
                    if(document.getElementById("arr_"+i+"_"+j).checked){
                        tempSelected++;
                    }
                }
            }
            if(tempSelected>1){
                alert("Maximum 1 per column allowed!")
                document.getElementById("arr_"+row+"_"+col).checked = false;
                return;
            }
        }
    
    
    }
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀