javascript - 为什么会出现此 'function is not defined' 错误?

标签 javascript jquery

<分区>

我正在尝试在我的项目中添加图像上传功能,我想在单击按钮时调用 jquery 函数。但我不断在控制台中收到此错误:

Uncaught TypeError: image is not a function at HTMLButtonElement.onclick

这是我的按钮:

<button id="upload" value="Change the picture" onclick="image()">Change the picture</button>

这是我驻留在 js 文件中的 jquery 函数:

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。

最佳答案

function image(){
});

您的代码中有一个额外的)。删除它。

function image(){
}

关于javascript - 为什么会出现此 'function is not defined' 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46125038/

相关文章:

jquery - 需要解释 jQuery .Show() 效果闪烁

javascript - float 图像

javascript 正则表达式,验证日期问题

javascript - 将字符串大写但不将前三个字母大写

javascript - 阻止重力作用下的弹跳球消失

javascript - 多选复选框不返回 Extjs 4.2 网格中的值

php - 如何使用 php 定位 mysql 查询表结果中的特定单元格

jquery - 有没有一种方法可以使用适用于所有浏览器的 jQuery 设置不透明度?

javascript - Jquery 显示 $20.099999999999998 而不是 $20.1

javascript - 使用 Angular 数据表动态更改列