jQuery serializeArray没有拾取动态创建的表单元素

Cyd*_*aps 6 html forms ajax jquery serializearray

我有一个使用ajax动态创建的表单(因为表单元素的数据必须来自数据库),我想序列化表单的元素以通过ajax提交.我目前只是使用jQuery网站上的代码来测试我的理论,看看我是否可以拿起表单元素,这就是问题所在:

$(document).ready(function() {
    $('#btnCustomSearch').live('click', function() {
            $('#results').html('');
            alert($('#customSearchTable :input').serializeArray());
            // get all the inputs into an array.
            var fields = $('#customSearchTable :input').serializeArray();
            jQuery.each(fields, function(i, field) {
                $("#results").append(field.name + " = " + field.value + ", ");
            });

            // now we'll reformat the data as we need

            // here we'll send the data via ajax

    });
});
Run Code Online (Sandbox Code Playgroud)

我需要在提交之前对数据进行一些更改并且此代码尚未编写,但我发现,页面加载时存在的页面上的任何输入元素都是正确的,任何元素都是使用Javascript填充正确,但是使用ajax创建的任何内容都将被忽略.

我知道这通常是使用"live"解决的,但我不知道如何解决这个问题serializeArray().使用Ajax会向其中添加其他表单元素#customSearchTable,这些元素是未被拾取的元素.

任何帮助非常感谢.

谢谢

Nic*_*ver 8

我将在这里详细阐述评论:

当你把.serializeArray()它称为循环时,就像<form>提交一样或尽可能接近提交要素,以便提交元素.关键部分在这里:

.filter(function() {
  return this.name && !this.disabled &&
         (this.checked || rselectTextarea.test(this.nodeName) ||
         rinput.test(this.type));
})
Run Code Online (Sandbox Code Playgroud)

正如<form>提交不包含没有name属性的元素一样,使用.filter()调用this.name将过滤那些要序列化的元素.