使用class为btn-check的radio单选框回显数据库数据无法切换如何解决
问题根源
label标签的for属性填写错误,for属性需要绑定对应单选框的id值,而非name值。你当前代码里两个label的for都写的是status_pendaftaran(单选框的name属性值),导致点击label无法正确触发对应单选框的选中切换,因此会卡在初始选中的状态。
修正后的代码
<?php foreach($isi as $u){ ?> <div> <input type="radio" class="btn-check" name="status_pendaftaran" value="1" <?php echo $u->status_pendaftaran=="1" ? 'checked' : ''; ?> id="buka" autocomplete="off"> <label class="btn btn-outline-success" for="buka">Pendaftaran dibuka</label> <input type="radio" class="btn-check" name="status_pendaftaran" value="2" <?php echo $u->status_pendaftaran=="2" ? 'checked' : ''; ?> id="tutup" autocomplete="off"> <label class="btn btn-outline-danger" for="tutup">Pendaftaran ditutup</label> </div> <?php }?>
改动点
- 第一个label的
for属性从status_pendaftaran改为buka,和第一个单选框的id对应 - 第二个label的
for属性从status_pendaftaran改为tutup,和第二个单选框的id对应
内容的提问来源于stack exchange,提问作者Agung Sedayu
相关产品推荐
相关产品推荐

