JQuery的阿贾克斯形式和动态创建的表单元素不提交表单、形式、素不、动态

2023-09-10 14:22:29 作者:洗头发的秃子

我正在写有动态加载一些文本输入的元素,当一个选择列表被改变的形式。

I'm writing a form that has some text input elements loaded dynamically when a select list is changed.

现在的问题是,当我提交表单的元素没有被发送到服务器发送的数据。

The problem is that when I submit the form those elements are not sent in the data that is posted to the server.

什么我需要做的就是这些动态创建的元素成的形式提交?

What do I need to do to get those dynamically created elements "into" the form to be submitted?

在code是这样的:

$("#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是:

The html returned by the ajax call is:

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

如果我使用Firebug调用AJAX方法后看页面的动态加载HTML是正确的层次结构中,它应该是...即它的形式。

And if I use firebug to look at the page after calling the ajax method the dynamically loaded html is right in the hierarchy where it should be...i.e. it's in the form.

但是,当我点击提交按钮存在的AJAX调用get之前发布形式的唯一元素。

But when I click the submit button only the elements of the form that existed before the ajax call get posted.

任何想法?

推荐答案

您的问题似乎是动态添加的输入元素没有名称属性。输入单元要求名称如果你希望自己的价值观被纳入提交的数据属性。

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.

 
精彩推荐