4

我正在编写一个表单,该表单在更改选择列表时会动态加载一些文本输入元素。

问题是当我提交表单时,这些元素不会在发布到服务器的数据中发送。

我需要做什么才能让这些动态创建的元素“进入”要提交的表单?

代码是这样的:

$("#my_select_id").change(function() {
    $.ajax({
        type: "GET",
        url: "some-url/" + $("#played_number_game_id").children("option:selected").val(),
        async: false, 
        dataType: "html",
        error: function(XMLHttpRequest, status, errorThrown) {
            alert("oh no!");
            alert(status);
        },
        success: function (data, status) {
            $("#parent-element").html("");
            $("#parent-element").append(data);
        },
        complete: function() {
        }
    });

    $("#my_form_submit").click(function() {
        $("#my-form").ajaxSubmit({ clearForm: true }); 
        return false;
    });
});

ajax调用返回的html是:

<input id="my-form_e_1" class="number-input" type="text"/>
<input id="my-form_e_2" class="number-input" type="text"/>

如果我在调用 ajax 方法后使用 firebug 查看页面,动态加载的 html 就在它应该在的层次结构中......即它在表单中。

但是当我单击提交按钮时,只有在 ajax 调用之前存在的表单元素才会被发布。

有任何想法吗?

4

2 回答 2

5

Your problem seems to be that the dynamically added input elements do not have name attributes. Input elements require name attributes if you want their values to be included in the submitted data.

于 2009-12-14T01:48:46.157 回答
0

It's pretty simple, just append the form element to the body tag and set its style to 'display:none'

于 2013-08-27T06:38:42.007 回答