复选框值到php数组

时间:2016-07-14 14:02:55

标签: javascript php jquery

我使用以下代码来确保至少选中一个复选框,它会将复选框的“值”输出到名为“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>

1 个答案:

答案 0 :(得分:0)

你需要什么阵列?

如果你这样做,你需要在加载和不允许删除复选框时继续设置它

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

&#13;
&#13;
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();
});
&#13;
<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>
&#13;
&#13;
&#13;