我有一个 php 和 html 代码,我尝试从我的复选框中取消选中。它可以工作,所以当它取消选中时,它会在 url 中显示 print=show ,当它选中时,它会显示 print=print 。但是当我检查这个时, print=show 与 URL 中的 print=print 站在一起。但只有一个人必须站在那里。
<div class="toggle text">
<label>
<?php
$printValue = (isset($_GET['show']) && $_GET['show'] == 'show') ? 'checked' : '';
echo '<input type="hidden" name="print" value="show" ' . $printValue . '>';
?>
<?php
$printValue = (isset($_GET['print']) && $_GET['print'] == 'print') ? 'checked' : '';
echo '<input type="checkbox" name="print" value="print" ' . $printValue . '><span class="slider"></span>';
?>
</label>
</div>
请不要使用 JS 或其他任何东西。仅 PHP 和 HTML。
一切! :) 我尝试询问 ChatGPT,但没有有效的答案。
如果我正确理解你的问题,我会说如果你只想一次检查一个,你应该使用单选而不是复选框:
<div class="toggle text">
<label>
<?php
$printValue = (isset($_GET['show']) && $_GET['show'] == 'show') ? 'checked' : '';
echo '<input type="radio" name="print" value="show" ' . $printValue . ' style="display:none">';
$printValue = (isset($_GET['print']) && $_GET['print'] == 'print') ? 'checked' : '';
echo '<input type="radio" name="print" value="print" ' . $printValue . '><span class="slider"></span>';
?>
</label>
</div>