我从这个函数中获取数据,现在我想要根据我的条件选中复选框,我的条件是如果我从数据库中获得值roro
,如果我获得Vessel, Containerized
,将不会选中"是"。
$getDataForPDF = getDataForPDF($bookingsId, $bookingsLettersDockReceiptId);
if($getDataForPDF["typeOfMove"] == "Vessel, Containerized") {
$checkedYes = "checked";
} if($getDataForPDF["typeOfMove"] == "roro") {
$checkedNo = "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 (Vessel only)</small> <br/>';
$html .= '<input type="checkbox" checked="'.$checkedYes.'" name="yes"> Yes <input type="checkbox" checked="'.$checkedNo.'" name="no"> No';
$html .= '</td>';
$html .= '</tr>';
我的条件是正确的,但不适用于checked="checked"
条件。我的代码出了什么问题?
checked属性就像在标记末尾添加checked一样简单。例如,<input type="checkbox" name="yes" checked>
将是一个复选框,其中不存在复选框将被取消选中。
以下代码应该为您解决问题:
$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 (Vessel only)</small> <br/>';
$html .= '<input type="checkbox" name="yes"'.$checkedYes.'> Yes <input type="checkbox" name="no"'.$checkedNo.'> No';
$html .= '</td>';
$html .= '</tr>';
JSFiddle:http://jsfiddle.net/o984L61e/
编辑
尝试将顶部代码块更改为:
$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
}
tcpdf上的示例与其他答案一样使用checked='checked'
属性,但在尝试连接html字符串之前,必须初始化变量。正如问题中所写的,$checkedYes
或$checkedNo
将是未定义的,因为它们在互斥的if
块中初始化。不确定这是否会产生影响,但这似乎是合理的。
你能试试吗?
$getDataForPDF = getDataForPDF($bookingsId, $bookingsLettersDockReceiptId);
if($getDataForPDF["typeOfMove"] == "Vessel, Containerized") {
$checkedYes = "checked='checked'";
} elseif($getDataForPDF["typeOfMove"] == "roro") {
$checkedNo = "checked='checked'";
}
连同:
$html .= '<input type="checkbox" '.$checkedYes.' name="yes"> Yes <input type="checkbox" '.$checkedNo.' name="no"> No';
我不确定它是否能解决你的问题,但这正是你想要做的,以便根据你的数据库信息相应地选中复选框。
它有帮助吗?
TCPDF的writeHTML()函数只支持有限的HTML标记子集。有关详细信息,请查看writeHTML()文档。input
标记显然是不支持的标记。
相反,您可以使用HTML实体来表示选中和未选中的复选框。已经有一个StackOverflow问题给出了您可以使用的可能实体的示例。