【问题标题】:display if checkbox is checked while editing如果在编辑时选中复选框,则显示
【发布时间】:2016-07-22 14:19:49
【问题描述】:

因为我已经经历了几个解决方案,所以我没有得到解决方案。当我去编辑并打开页面时,我想显示我在插入时选择的复选框列表。 这是我的视图页面..

<div class="form-group">
        <label class="control-label col-sm-1" for="courses">Courses Completed</label>
         <div class=" col-sm-5">

         <input type="checkbox"  name="select_course[]" id="select_course" value="10" <?php if($row->courses =='10') echo "checked" ;?>>10
         <input type="checkbox"  name="select_course[]" id="select_course" value="12" <?php if($row->courses =='12') echo "checked" ;?>>12
         <input type="checkbox"  name="select_course[]" id="select_course" value="degree" <?php if($row->courses=='degree') echo "checked" ;?>>degree

         </div>
    </div>

通过这样做,没有一个复选框显示为选中状态。插入的值就像一个数组,我的表看起来像这样

id  name        address             sex     courses     places                 image            password
 1  nesru         v                 male    "10"        tamilnadu   upld-file1469095130.jpg       l
 3  siraj   koonathil house        male     "10,12"     tamilnadu   upld-file1469167954.jpg       d

【问题讨论】:

  • 你能打印$row->课程
  • 显示print_r($row-&gt;courses);的输出

标签: php codeigniter checkbox


【解决方案1】:

根据您发布的表格,您似乎在课程中存储逗号分隔值,因为您需要在数组中转换并使用in_array() 函数进行检查

<div class="form-group">
    <label class="control-label col-sm-1" for="courses">Courses Completed</label>
   <?php $idArray = explode(",",$row->courses); ?>
    <div class=" col-sm-5">
        <input type="checkbox"  name="select_course[]" id="select_course" rel="<?php echo $row->courses; ?>" value="10" <?php if(in_array('10',$idArray)) echo "checked" ;?>>10
        <input type="checkbox"  name="select_course[]" id="select_course" rel="<?php echo $row->courses; ?>" value="12" <?php if(in_array('12',$idArray)) echo "checked" ;?>>12
        <input type="checkbox"  name="select_course[]" id="select_course" rel="<?php echo $row->courses; ?>" value="degree" <?php if(in_array('degree',$idArray)) echo "checked" ;?>>degree
    </div>
</div>

【讨论】:

  • 我使用 json_encode 插入值,所以需要解码它?
  • 我已经解码它得到它......非常感谢......像这样改变 courses)); ?>
【解决方案2】:

checked状态的属性是checked=""

把你的php代码放在引号里

 <input type="checkbox"  name="select_course[]" id="select_course" value="degree" checked="<?php if($row->courses=='degree') echo "checked" ;?>" />

【讨论】:

  • 我给了这样的,但所有的复选框都显示为标记
  • @MOHAMMED 你必须向我们展示print_r($row-&gt;courses); 的输出,否则每个人都只是猜测。在您的代码中输出推杆
  • 那我就不得不同意上面的评论了你需要print_r的内容($row->courses);听起来错误在于从数据库中检索数据或从任何地方提取数据
  • 我的 print_r($row->courses);看起来像这样“10,12”
【解决方案3】:

你应该输入以下代码:

<input type="checkbox"  name="select_course[]" id="select_course" value="degree" <?php if($row->courses=='degree'): ?> checked="checked" <?php endif; ?> />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-14
    • 2013-06-10
    • 1970-01-01
    相关资源
    最近更新 更多