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 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程