【问题标题】:display data only on selecting a checkbox仅在选中复选框时显示数据
【发布时间】:2016-01-29 02:17:20
【问题描述】:

我有一个复选框,并且希望当它被选中/选中时,应该加载来自另一个文件(即 c_datetime.php)的值,为此我使用了 ajax,当我选中/选中复选框时,值是getting loaded properly but when i uncheck it the loader gets loaded again and the ajax displays the data again, whereas the proper way should have been that when the checkbox is selected the values should get loaded and when its unselected the values should disappear, can anyone请说明为什么它没有按应有的方式工作

<script>
$(document).ready(function(){
   $('#drive').change(function(){

        var catid = $('#drive').val();
        console.log($('#drive'))
        if(catid != 0)

        {
            LodingAnimate();
            $.ajax({
                type:'post',
                url:'c_datetime.php',
                data:{id:catid},
                cache:false,
                success: function(returndata){
                    $('#datetime').html(returndata);
                console.log(returndata)
                }
            });

             function LodingAnimate() 
                {
                    $("#datetime").html('<img src="img/ajax-loader.gif" height="40" width="40"/>');
                } 
        }
    }
    )
}) 

</script>   


<div id="datetime"> </div>
<input type="checkbox" name ="chk" id="drive" value="Drive" >Drive

【问题讨论】:

  • 你应该检查复选框是否只选中然后加载数据添加if($(this).is(':checked')) condition

标签: javascript php jquery ajax checkbox


【解决方案1】:
    $(document).ready(function () {
    $('#drive').change(function () {
        if ($(this).is(':checked')) {
            var catid = $('#drive').val();
            console.log($('#drive'))
            if (catid != 0)

            {
                LodingAnimate();
                $.ajax({
                    type: 'post',
                    url: 'c_datetime.php',
                    data: {
                        id: catid
                    },
                    cache: false,
                    success: function (returndata) {
                        $('#datetime').html(returndata);
                        console.log(returndata)
                    }
                });

                function LodingAnimate() {
                    $("#datetime").html('<img src="img/ajax-loader.gif" height="40" width="40"/>');
                }
            }
        }else{
            $("#datetime").hide();
        }
    })
})

试试这个

demo

你应该检查复选框是否只选中然后加载数据添加if($(this).is(':checked'))条件

【讨论】:

  • 它在选中复选框时显示数据,但在取消选中时不隐藏内容
  • 您在 if 条件的 else 部分执行此操作。我没有包括它你可以做到
【解决方案2】:

尝试在change 事件处理程序中使用if 条件this.checked。 如果 #drive 属性 checked 返回 true ,则调用 $.ajax() ,否则不要调用 $.ajax()

$(document).ready(function() {
  $('#drive').change(function() {
    if (this.checked) {
      // do `$.ajax()` stuff
      console.log(this.checked)
    } else {
      // do not do `$.ajax()` stuff
      console.log("not checked")
    }
  })
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="checkbox" name="chk" id="drive" value="Drive">Drive

【讨论】:

  • 我还需要在未选中复选框时隐藏数据
  • @kavi “我还需要在未选中复选框时隐藏数据” 可以将隐藏数据的 js 放置在 stacksn-ps 的 else 语句中。当input type="checkbox" checked 属性在this.checked 处返回false 时,应调用else 中的js 语句
【解决方案3】:

这就是你要找的东西:-

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Show Hide Using Checkboxes</title>
<style type="text/css">
.box{
padding: 20px;
display: none;
margin-top: 20px;
}
</style>
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('input[type="checkbox"]').click(function(){
if($(this).attr("value")=="red"){
$(".red").toggle();
}
});
});
</script>
</head>
<body>
<div>
<label><input type="checkbox" name="colorCheckbox" value="red"> Drive</label></div>
<div class="red box">
<img src="Koala.jpg" alt="Mountain View" style="width:304px;height:228px;"></div>
</body>
</html> 

【讨论】:

    【解决方案4】:
    <form action="#" method="post">
        <input type="checkbox" name="check_list[]" value="C/C++"><label>C/C++</label><br/>
        <input type="checkbox" name="check_list[]" value="Java"><label>Java</label><br/>
        <input type="checkbox" name="check_list[]" value="PHP"><label>PHP</label><br/>
        <input type="submit" name="submit" value="Submit"/>
    </form>
    <?php
        if(isset($_POST['submit'])){//to run PHP script on submit
            if(!empty($_POST['check_list'])){
                // Loop to store and display values of individual checked checkbox.
                foreach($_POST['check_list'] as $selected){
                    echo $selected."</br>";
                }
            }
        }
    ?>
    

    【讨论】:

    • 您可以在此处为您的代码添加一点解释。
    猜你喜欢
    • 1970-01-01
    • 2016-02-23
    • 1970-01-01
    • 2013-03-13
    • 2012-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多