简体   繁体   中英

Why when checked checkbox it's will show incorrect data value?

Why when checked checkbox it's will show incorrect data value ?a ?

First, Load page index.php it's will display All it's OK.

But when you checked checkbox Red it's will display All and Red

Why not show only Red ?

index.php

<script src="http://code.jquery.com/jquery-1.7.2.js"></script>
<script>
$("#f_id").submit(send_data_requests());
</script>

<form method="post" id="f_id" action="Javascript:void(0);" >
    <input type="checkbox" id="all_color" name="all_color" value="All" onclick="send_data_requests()" checked >All<br>
    <input type="checkbox" id="color_red" name="color_red" value="Red" onclick="send_data_requests()">Red<br>
    <input type="checkbox" id="color_blue" name="color_blue" value="Blue" onclick="send_data_requests()">Blue<br>
</form>

<div id="data_areas">

<script type="text/javascript">
$(window).load(function(){
$('#all_color').click(function () {   
     $(this).prop("checked", "checked");
     $('input:checkbox').not(this).prop('checked', !this.checked);           
 });

$("#color_red").click(function() {
    $("#all_color").prop('checked', false);
});

$("#color_blue").click(function() {
    $("#all_color").prop('checked', false);
});

});

</script>

<script>
function send_data_requests()
    {
        $('#data_areas').hide();
        $.ajax
            (
                {
                    url: 'datas.php',
                    type: 'POST',
                    data: $('#f_id').serialize(),
                    cache: false,
                    success: function(data)
                        {
                            $('#data_areas').show();
                            $('#data_areas').html(data);
                        }
                }
            );
        return false;
    }
// on load page call function code //
$(document).ready(send_data_requests());
</script>

datas.php

<?php
echo $_POST[all_color];
echo "<BR>";
echo $_POST[color_red];
echo "<BR>";
echo $_POST[color_blue];
?>

Try $('#all_color').removeAttr('checked');

instead of $("#all_color").prop('checked', false);

fiddle .

For some reason (which i would like to know as well*) the prop method does not remove the "checked" attribute from the checkbox, even though it un-checks it.

Edit: *seems like this gives an explanation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM