【问题标题】:Do not show checkbox value if checkbox not checked如果未选中复选框,则不显示复选框值
【发布时间】:2013-03-27 21:18:36
【问题描述】:

我有以下代码用于显示一些自定义字段。

<table>
    <tbody>
<?php 
foreach ($fields as $field) {
$type = $field['s_type'];
$label = $field['s_label'];
$value = Attributes::newInstance()->getValue($item_id, $field['pk_id']);
if ($type == 'checkbox') {
    if ($value == 'checked') $value = 'Yes';
    else $value = 'No';
}
?>

        <tr>
            <td class='detail_label'><?php echo $label; ?></td>
            <td class='detail_label'><?php echo $value; ?></td>
        </tr>
<?php } ?>
    </tbody>
</table>

当复选框值 = 'No' 时,我不需要在 tr 中显示内容

有可能吗?

感谢您的帮助!

【问题讨论】:

  • 不显示什么内容?
  • tr 中的内容。现在它返回“Label1:Yes,Label2:No,Label3:No”,但我只想显示“Label1:Yes”而不是“No”值的其他人
  • 为什么不将表格行包装在判断它是否有值的 if 语句中?

标签: php if-statement checkbox


【解决方案1】:

为什么不将tr 包装在 if 语句中?您可以重复使用相同的 $value 变量,因为您只想在“是”时显示该值。

<?php
    if ($value == 'Yes')
    {
?>
        <tr>
            <td class='detail_label'><?php echo $label; ?></td>
            <td class='detail_label'><?php echo $value; ?></td>
        </tr>
<?php
    }
?>

【讨论】:

  • 感谢您的代码...我将其更改为 !=='No' 因为我也有不同的字段类型
【解决方案2】:

试试这个

 <table>
    <tbody>
<?php 
foreach ($fields as $field) {
   $displayflag=true; //<--here
$type = $field['s_type'];
$label = $field['s_label'];
$value = Attributes::newInstance()->getValue($item_id, $field['pk_id']);
if ($type == 'checkbox') {
    if ($value == 'checked'){ $value = 'Yes'; }
    else{ $value = 'No';$displayflag=false}; //<--here
}
?>
 <?php if($displayflag){?> //<--here

        <tr>
            <td class='detail_label'><?php echo $label; ?></td>
            <td class='detail_label'><?php echo $value; ?></td>
        </tr>
  <?php }?>//<--here
<?php } ?>
    </tbody>
</table>

【讨论】:

    猜你喜欢
    • 2015-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多