未选中所选复选框。为什么?

时间:2016-11-09 05:32:30

标签: php jquery checkbox

checkbox.php

<body>

<form>

  <label>
    <input type="checkbox" value="1" name="name">fruits</label>
  <label>
    <input type="checkbox" value="2" name="name">vegitables</label>

</form>

<!--modal-->
<div id="myModal" class="modal fade" role="dialog">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Modal Header</h4>
      </div>
      <div class="modal-body">

      </div>
      <div class="modal-footer">
        <button type="submit" class="btn btn-default" id="submit" onclick="submit();">save</button>
        <h4 id="result"></h4>

      </div>
    </div>
  </div>
</div>



</body>

以上是我的两个主要复选框。单击复选框时会出现模态。在其上显示来自数据库的动态复选框(子复选框)。当我点击模态上的子复选框,然后单击提交。警报弹出没有值。我需要做的是将选中的复选框值转换为警报。有谁可以帮助我?

以下是我的 checkbox.js

 $(document).ready(function(){
    $("input[type=checkbox][name=name]").change(function(){
     if(this.checked) {

        var value = $(this).val();
        $.ajax({
            url:"modal.php",
            type:"POST",
            data:{value:value},
            success:function(modalBody){
                $("#myModal .modal-body").html(modalBody);
                $("#myModal").modal('show');
              }

        });
      }
       });




});


 function submit() {

     var values = [];
     $('input[type=checkbox][name=sub]').each(function(){
         if($(this).is(":checked"))
           {
      values.push($(this).val()); 
       alert(values.join());       
         }
        });

}

modal.php

<html>
<body>

<?php   

            if($_POST['value']){
               $test = $_POST['value'];
               include("config.php");

            $sql = "SELECT name FROM tb where Id=".$value." ";
            $res = mysqli_query($conn,$sql);
            while($row = mysqli_fetch_assoc($res)){
?>
            <input id='category' type='checkbox' name=sub'[]' value = <?php echo $row['name']; ?> /><?php
            echo $row['name'];
            echo "<br>";

    }

}

else echo "not post";

?>

</body>
</html>

的config.php

<?php
            $servername = "localhost";
            $username = "root";
            $password = "";
            $db = "test";


    // Create connection
            $conn = mysqli_connect($servername, $username, $password,$db);


    // Check connection
            if (!$conn) {
            die("Connection failed: " . mysqli_connect_error());
            }
            echo "Connected successfully <br/>";
?>      

1 个答案:

答案 0 :(得分:1)

修复您的php页面

    <?php   
             if($_POST['value']){
                   $test = $_POST['value'];
                   include("config.php");

                $sql = "SELECT name FROM tb where Id=".$value." ";
                $res = mysqli_query($conn,$sql);
                while($row = mysqli_fetch_assoc($res)){
                echo "<input class='category' type='checkbox' name='sub[]' value ='".$row['name'];."'/>";
                echo $row['name'];
                echo "<br>";

        }

    }

else echo "not post";

?>

你的js

  var values = [];
     $('.category:checked').each(function(){
           values.push($(this).val()); 
        });
    alert(values.join());