简体   繁体   English

为什么当选中此复选框时它将显示不正确的数据值?

[英]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. 首先,加载页面index.php ,它将显示All正常。

But when you checked checkbox Red it's will display All and Red 但是,当您选中Red复选框时,它将显示AllRed

Why not show only Red ? 为什么不只显示Red

index.php 的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 datas.php

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

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

instead of $("#all_color").prop('checked', false); 而不是$("#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. 由于某种原因(我也想知道*),prop方法不会从复选框中删除“ checked”属性,即使该属性未选中也是如此。

Edit: *seems like this gives an explanation 编辑: *似乎像这样给一个解释

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 选中时,为什么复选框属性显示为“ false”(未选中)? - Why do checkbox property show as 'false' (not checked) when checked? Angular Js在页面加载时显示复选框已选中 - Angular Js Show the Checkbox's Checked when Page Loads 选中复选框后,更改所有下拉选项的值 - Change all the dropdown option' s value when the checkbox is checked 为什么当未选中接受条款复选框或未选择性别时,我的 email 也显示为不正确? - Why when the accept terms checkbox is not checked or the gender is not chosen then also my email shows up as incorrect? 选中时复选框值不会更改 - Checkbox value will not be changed when checked 复选框值仅在选中时 - Checkbox value only when checked 当值与来自 database-javascript 的数据相同时选中复选框 - checkbox checked when value same with data from database-javascript 使用涉及“ checkbox.checked === true”的if / then命令在涉及另一个复选框时隐藏/显示元素时出现问题 - Problem using if/then command with “checkbox.checked === true” to hide/show an element when there's another checkbox involved 为什么复选框选中后会自动选中下一个复选框 - Why checkbox automatically checked the next checkbox when checked 为什么复选框值在未选中时打开 - Why checkbox value is on when it's unchecked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM