douzhi7070 2015-03-27 15:55
浏览 38
已采纳

复选框条件无法正常工作

I'm getting data from this function, now I want checked checkboxes based on my condition and my condition is if I got value roro from db no will be checked if I got Vessel, Containerized yes will be checked.

$getDataForPDF = getDataForPDF($bookingsId, $bookingsLettersDockReceiptId);
if($getDataForPDF["typeOfMove"] == "Vessel, Containerized") {
     $checkedYes = "checked";
} if($getDataForPDF["typeOfMove"] == "roro") {
     $checkedNo = "checked";
}

and I'm using this condition in checkboxes

$html .= '<tr>';
     $html .= '<td colspan="3">';
          $html .= '<small>11. TYPE OF MOVE</small> <br />';
          $html .= $getDataForPDF["typeOfMove"];
     $html .= '</td>';

     $html .= '<td colspan="2">';
          $html .= '<small>11a. CONTAINERIZED &nbsp;&nbsp;&nbsp; (Vessel only)</small> <br/>';
          $html .= '<input type="checkbox" checked="'.$checkedYes.'" name="yes"> Yes &nbsp;&nbsp;&nbsp; <input type="checkbox" checked="'.$checkedNo.'" name="no"> No';
      $html .= '</td>';
$html .= '</tr>';

my condition is right but not working on checked="checked" condition. What is wrong in my code?

  • 写回答

3条回答 默认 最新

  • dongmu4591 2015-03-27 16:09
    关注

    The checked property is as simple as adding checked at the end of the tag. For example <input type="checkbox" name="yes" checked> would be a checked box where the absence of checked would be unchecked.

    The following code should solve the problem for you:

    $checkedYes = '';
    $checkedNo = '';
    $getDataForPDF = getDataForPDF($bookingsId, $bookingsLettersDockReceiptId);
    if($getDataForPDF["typeOfMove"] == "Vessel, Containerized") {
         $checkedYes = " checked";// note the space before checked
    } if($getDataForPDF["typeOfMove"] == "roro") {
         $checkedNo = " checked";// note the space before checked
    }
    
    
    $html .= '<tr>';
         $html .= '<td colspan="3">';
              $html .= '<small>11. TYPE OF MOVE</small> <br />';
              $html .= $getDataForPDF["typeOfMove"];
         $html .= '</td>';
    
         $html .= '<td colspan="2">';
              $html .= '<small>11a. CONTAINERIZED &nbsp;&nbsp;&nbsp; (Vessel only)</small> <br/>';
              $html .= '<input type="checkbox" name="yes"'.$checkedYes.'> Yes &nbsp;&nbsp;&nbsp; <input type="checkbox"  name="no"'.$checkedNo.'> No';
          $html .= '</td>';
    $html .= '</tr>';
    

    JSFiddle: http://jsfiddle.net/o984L61e/

    Edit

    Try changing the top code block to:

    $checkedYes = '';
    $checkedNo = '';
    $getDataForPDF = getDataForPDF($bookingsId, $bookingsLettersDockReceiptId);
    if($getDataForPDF["typeOfMove"] == "Vessel, Containerized") {
         $checkedYes = " checked='checked'";// note the space before checked
    } 
    if($getDataForPDF["typeOfMove"] == "roro") {
         $checkedNo = " checked='checked'";// note the space before checked
    }
    

    The examples on tcpdf use the checked='checked' property just like the other answers had, but you must initialize the variables before you try to concatenate in the html string. As written in the question, either $checkedYes or $checkedNo will be undefined since they are being initialized in mutually exclusive if blocks. Not sure if this will make a difference but it seems plausible.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 bywave配置文件写入失败
  • ¥20 基于Simulink的ZPW2000轨道电路仿真
  • ¥15 pycharm找不到在环境装好的opencv-python
  • ¥15 在不同的执行界面调用同一个页面
  • ¥20 基于51单片机的数字频率计
  • ¥50 M3T长焦相机如何标定以及正射影像拼接问题
  • ¥15 keepalived的虚拟VIP地址 ping -s 发包测试,只能通过1472字节以下的数据包(相关搜索:静态路由)
  • ¥20 关于#stm32#的问题:STM32串口发送问题,偶校验(even),发送5A 41 FB 20.烧录程序后发现串口助手读到的是5A 41 7B A0
  • ¥15 Mabatis查询数据
  • ¥15 想知道lingo目标函数中求和公式上标是变量情况如何求解