【问题标题】:Unable to access form data inside ajax loaded data with jquery无法使用 jquery 访问 ajax 加载数据中的表单数据
【发布时间】:2019-03-08 17:14:41
【问题描述】:

我有一个动态 ajax 加载表单,如下所示:

<form id="ProtocolForm">
<input id="name_major" type="text" value="some value">
<input id="name_minor" type="text" value="some value">
<input id="Submit" type="submit" value="Submit Protocol">
</form>

在加载ajax内容的主页面中,我有以下功能:

$(document).on( "submit", "#ProtocolForm", function( event ) {
event.preventDefault();
alert('submit intercepted:' + $( this ).serialize() );
});

我的问题是我似乎无法从这个函数内部访问实际的表单数据。该函数被适当地调用,但是 $( this ).serialize() 和 $("ProtocolForm").serialize() 似乎都是空白的。

任何帮助将不胜感激。 谢谢!

【问题讨论】:

  • 输入没有 name 属性...这就是为什么 .serialize() 什么也抓不到。
  • 天哪,我真是太愚蠢了。谢谢!
  • 这不是“愚蠢”...你问得很好。 ;)

标签: javascript jquery ajax forms


【解决方案1】:

您的问题与动态内容无关。您正确使用了委托。

但是.serialize() 依赖name 属性来收集表单数据...只需添加它们。

;)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-19
    相关资源
    最近更新 更多