You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JavaScript undefined数据问题:表单数据无法存入数据库排查

问题原因

你的serializeObject函数存在笔误:所有引用表单字段名的地方都写了this.age_utente,但你的表单里根本没有名为age_utente的输入项,导致this.age_utente的值始终是undefined,最终生成的JSON会把所有字段值都归类到"undefined"这个异常键下。

修复方案

将serializeObject函数里的this.age_utente全部替换为this.name——因为jQuery的serializeArray()方法返回的每个对象,都包含name(对应表单元素的name属性)和value(对应表单元素的值)两个核心属性。

修复后的完整函数代码:

$.fn.serializeObject = function() {
  let o = {};
  let a = this.serializeArray();
  $.each(a, function() {
    if (o[this.name] !== undefined) {
      if (!o[this.name].push) {
        o[this.name] = [o[this.name]];
      }
      o[this.name].push(this.value || '');
    } else {
      o[this.name] = this.value || '';
    }
  });
  return o;
};
额外提示
  • 调试时可以先打印serializeArray()的返回结果,确认每个对象的结构,能更快定位这类字段名错误
  • 也可以直接使用成熟的表单序列化工具(比如jQuery的第三方序列化插件),减少手写函数的出错概率

内容的提问来源于stack exchange,提问作者Edoardo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 21:45:37