私は自分のプロジェクトに画像のアップロード機能を追加しようとしています。ボタンをクリックするとjquery関数が呼び出されます。しかし、私は、コンソールでこのエラーを取得しておいてください。なぜこの「関数は定義されていません」というエラーが表示されますか?
<button id="upload" value="Change the picture" onclick="image()">Change the picture</button>
そして、これは、JSファイルに存在する私のjqueryの関数である:
Uncaught TypeError: image is not a function at HTMLButtonElement.onclick
これが私のボタンである
function image() {
var file_data = $('#file').prop('files')[0];
var form_data = new FormData();
form_data.append('file', file_data);
$.ajax({
url: 'index.php/welcome/upload_image' + '/' + ID, // point to server-side controller method
dataType: 'text', // what to expect back from the server
cache: false,
contentType: false,
processData: false,
data: form_data,
type: 'post',
success: function (response) {
$('.messages').html(response); // display success response from the server
},
error: function (response) {
$('.messages').html(response); // display error response from the server
}
});
});
PS:私は自分のプロジェクトでCodeIgniterを使用しています。
あなたのページにJSとjqueryをどうやって入れていますか? – Alfabravo
ページに 'image'の' id'要素がありますか? – Ryan
私はそれらをボトムの最後のbodyタグの直前に含めました。私はjqueryをダウンロードしていない、私はCDNを使用しています。 – eli