简体   繁体   English

即使未选中,复选框也始终选中

[英]Checkbox is always checked even not checked

I have a checkbox that is always checked no matter if I checked the box or not.我有一个复选框,无论我是否选中该复选框,它始终处于选中状态。

Here is how the checkbox is currently setup:以下是复选框当前的设置方式:

    <input class="classname" id="check"  value="1" <?php echo (($temp['test']) ? 'checked' : '');  ?> name="check" type="checkbox" />

Here is how the check box value is being captured:以下是捕获复选框值的方式:

     $check = $_POST['check'] ?? 0;

I also tried the following code as well:我也尝试了以下代码:

      $check = (isset($_POST['check']) == '1' ? '1' : '0');

I tried the hidden field approach as well and it didn't work.我也尝试了隐藏字段方法,但没有奏效。 Any ideas on why the checkbox is showing marked even though I removed the check.即使我删除了检查,关于为什么复选框显示标记的任何想法。

I think Drussy might be onto something, where does $_POST['test'] come from?我认为 Drussy 可能在做某事,$_POST['test'] 来自哪里?

Try this code:试试这个代码:

$checked = isset($_POST['check']) ? 'checked' : '';

<input class="classname" id="check"  value="1" <?php echo $checked; ?> name="check" type="checkbox" />

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

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