目前,我正在嘗試在 codeigniter 的視圖檔案夾下撰寫一個 AJAX 腳本。為此,我曾經<?= $this->section('scripts')?>在 php 檔案中創建一個腳本。但是這樣做會給我帶來致命錯誤:呼叫未定義的方法 CI_Loader::section()。這是我的完整代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>CodeIgniter MVC Basics</title>
</head>
<body>
<form method='post' action="index">
<h1>Customer Details</h1>
<input type="date" name="startDate"/>
<input type="date" name="endDate"/>
<select name="status">
<option>Draft</option>
<option>Unpublish</option>
<option>Let</option>
</select>
<br/><br/>
<button type="button" class="alertbox" name="alertbox">alertbox</button>
</form>
</body>
</html>
<?= $this->section('scripts')?>
<script>
$(document).ready(function(){
$(document).on('click', '.alertbox', function(){
alert("Hello world");
});
});
</script>
<?= $this->endsection()?>
我想要它,以便每當單擊按鈕時,螢屏上都應該有一個警報彈出視窗。
編輯我正在從我的控制器加載視圖,如下所示:
$this->load->view('crm/test');
以下是錯誤:

uj5u.com熱心網友回復:
這段代碼:
$this->load->view('crm/test');
是在 Codeigniter 3 中加載視圖的方式。
這段代碼:
<?= $this->section('scripts')?>
...
<?= $this->endsection()?>
正在使用來自 Codeigniter 4 的布局。所以看起來您正在使用 CI4,但如該布局頁面所示,在主視圖檔案中,在 CI4 中呈現和顯示視圖的方式是不同的:
echo view('crm/test');
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/315055.html
