复选框值为 php 数组


Checkbox value to php array

我使用以下代码来确保至少选中其中一个复选框,它将复选框的"值"输出到名为"cont"的 ID。我正在寻找一种将输出转换为数组 (PHP) 的方法。我不太擅长JS。

<?php
$count = '0';
while($count <= '2') {
    $count++;
    $the_name = "chkBx";
    $the_value = 'checkbox'.$count;
    if ($count == '1') {
        $its_checked = 'checked'; 
    } else { 
        $its_checked = ''; 
    }
    ?>
    <input type="checkbox" name="<?php echo $the_name; ?>" value="<?php echo $the_value; ?>" <?php echo $its_checked; ?> > 
    <?php
}
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="cont"></div><!-- OUTPUTS HERE-->
<script>
$('input[type="checkbox"][name="<?php echo $the_name; ?>"]').on('change',function(){
  var getArrVal = $('input[type="checkbox"][name="<?php echo $the_name; ?>"]:checked').map(function(){
    return this.value;
  }).toArray();
  if(getArrVal.length){
    //execute the code
    $('#cont').html(getArrVal.toString());
  } else {
    $(this).prop("checked",true);
    $('#cont').html("At least one value must be checked!");
    return false;
  };
});
</script>
#

更新,仍然无法从勾选到 PHP 变量中的复选框中获取值,这是我现在拥有的:

<?php
$the_name = "myCheck";
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
1.<input type="checkbox" class="<?php echo $the_name; ?>" name="the_name" value="1" checked>
2.<input type="checkbox" class="<?php echo $the_name; ?>" name="the_name" value="2">
3.<input type="checkbox" class="<?php echo $the_name; ?>" name="the_name" value="3"><br/>
<span class="cont" name="bob" value="9999"></span>
<script>
function checkIt() {
    var getArrVal = $(".myCheck:checked").map(function() {
      return this.value;
    }).get();
    if (getArrVal.length) {
        $('.cont').html(getArrVal.join(","));
        var x=$(".cont").text();
        var variablename = $(".cont").val();
    } else {
      $('.cont').html("At least one value must be checked!");
      $(this).prop("checked", true);
      getArrVal=[$(this).val()];
      return false; 
    }
    console.log(getArrVal);
}
$(function() {
  $(".myCheck").on('click',checkIt);
  checkIt();
});
</script>

你需要这个数组做什么?

如果这样做,则需要继续设置它,无论是在加载时还是在不允许删除复选框时

我个人会在 chekcbox 上使用一个类来制作一个更短的脚本:

function checkIt() {
    var getArrVal = $(".myCheck:checked").map(function() {
      return this.value;
    }).get();
    if (getArrVal.length) {
      $('#cont').html(getArrVal.join(","));
    } else {
      $('#cont').html("At least one value must be checked!");
      $(this).prop("checked", true);
      getArrVal=[$(this).val()];
      return false; 
    }
    console.log(getArrVal);
}
$(function() {
  $(".myCheck").on('click',checkIt);
  checkIt();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
1.<input type="checkbox" class="myCheck" name="the_name" value="1" checked> 2.<input type="checkbox" class="myCheck" name="the_name" value="2">
3.<input type="checkbox" class="myCheck" name="the_name" value="3"><br/>
<span id="cont"></span>