我正在使用PHP Codeigniter并嘗試使用onClick()將陣列從控制器傳遞到JavaScript函式,但找不到任何解決方案。
這是我在控制器中的代碼:
function get_class_students_mass_pdf($class_id) {
$students = $this->db->get_where('enroll', array(
'class_id' => $class_id, 'year' => $this->db->get_where('settings', array('type' => 'running_year'))->row()->description
))->result_array();
$invoiceid = array();
foreach ($students as $row) {
$invoice_id = $this->db->get_where('invoice', array('student_id' => $row['student_id']))->row()->invoice_id;
$invoiceid[] = $invoice_id;
}
$invoiceid = json_encode($invoiceid);
echo '<br><br><button style="margin-left: 36px;" type="button" onClick="invoice_view_modal('.$invoiceid.')"> ' . get_phrase('generate_pdf') . ' </button>';
}
視圖中的我的 JavaScript 函式:
function invoice_view_modal(invoiceid) {
showAjaxModal('<?php echo site_url('modal/popup/modal_view_mass_invoice/');?
>' invoiceid);
}
$invoiceid是我試圖在我的 JavaScript 函式中使用onClick()方法訪問的陣列。如何訪問它?
uj5u.com熱心網友回復:
我在我的專案中遇到了同樣的問題,在除錯后我才知道 javaScript 函式不接受陣列中的特殊字符,如","。所以我只是使用htmlspecialchars()它適用于我的。嘗試這個,
$invoiceid = htmlspecialchars(json_encode($invoiceid));
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/514626.html
