duanmao1319 2017-05-01 11:04
浏览 18

使用php检查复选框的值

I want to check the checkBox's value in php. Either selected or not. I am trying in a way;

JQuery

<script type="text/javascript">

    $(document).ready(function(){
        $("#save").click(function(){
            var checkBox = $("#boxV").is(':checked');

            $.ajax({
                url: "http://localhost/testing/test.php",
                type:"POST",
                async:true,
                data:{
                    "done":1,
                    "checkBox" : checkBox
                },
                success: function(data){
                        $('#result').append(data);
                }   
            });
        });
    });



 </script>

PHP

<?php
    if(!isset($_POST['checkBox'])){
        echo ('0');
    }else{
        echo ('1');
    }
    ?>

HTML

 <li><input type="checkbox" id = "boxV" > Check</li><br>

The problem is that both times (Selected , not selected) the result div is filled with 1. Is there any problem?

  • 写回答

5条回答 默认 最新

  • dongroufan6846 2017-05-01 11:18
    关注

    You can pass checkBox variable conditionally as describe below:

    $(document).ready(function(){
            $("#save").click(function(){
                var checkBox = $("#boxV").is(':checked');
                if(checkBox) {
                    var data = {
                       'done': 1,
                       'checkBox': checkBox 
                    };
                }else{
                     var data = {
                       'done': 1,                       
                     };
                }
                $.ajax({
                    url: "http://localhost/testing/test.php",
                    type:"POST",
                    async:true,
                    data:data,
                    success: function(data){
                         $('#result').append(data);
                    }   
                });
            });
        });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用